iosswiftxcode-ui-testingxcuitest

How to implement XCUITest with API Calls


I am new to XCUITest framework. I need to call an API https://restcountries.eu/rest/v2/all getting list of countries and showing in UITableView now as user will click on any cell based on country name next screen will have country name and country flag every thing is working fine now I wanted to implement XCUITest for that I have added function

 [![enter image description here][1]][1]

func testTheCountryListAndAllScreen() {
app.launch()
let countryListTableView = self.app.tables["CountryList"]
XCTAssertTrue(countryListTableView.exists, "Country list have data")
let countryListCell = countryListTableView.cells
 if countryListCell.count > 0 {
     for cell in 0..<countryListCell.count {
          let countryCell = countryListCell.element(boundBy: cell)
           countryCell.tap()
           self.app.buttons.staticTexts["Move to info page"].tap()
           self.app.navigationBars.buttons.element(boundBy: 0).tap()
           self.app.navigationBars.buttons.element(boundBy: 0).tap()
           }
      }
}

at app.launch() app is getting crashed


Solution

  • You can make requests with URLSession and access data with JSONEncoder.


    If you want to test your table (while changing its data), use unit tests. If you want to test how interaction with this table affects other UI Elements, use UI tests. For a better understanding about testing in Xcode watch these videos:


    You did not provide the error text of app launching crash, but I think you tried to use XCTest UI testing APIs in a unit test target.