I am new on Xcode + ios testing. I am going to automate ui of an existing ios project in swift. The problem is that I am unable to find table view of a view. Here is my code which i used to finding a cell from table view but this is not working in this case:
XCUIApplication().tables.cells.allElementsBoundByIndex[1].tap()
I am attaching screenshot of the view flow. The ios code of view is not written by me.
So you want to find a tableView's cell and then tap on it.
This solution is posted after testing in Xcode 9.0 beta 6, using Swift 4
As mentioned by Oletha in a comment, it's a good idea to understand the hierarchy of your view by printing the debugDescription:
let app = XCUIApplication()
override func setUp() {
super.setUp()
continueAfterFailure = false
app.launch()
print(app.debugDescription)
}
The print statement will give you a detailed list of views and their hierarchy.
Next, you should add an accessibilityIdentifier to your tableview and cell as follows:
a) Inside your viewDidLoad() or any other relevant function of the controller-
myTableView.accessibilityIdentifier = “myUniqueTableViewIdentifier”
b) Inside your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) function-
cell.accessibilityIdentifier = "myCell_\(indexPath.row)"
Once done, you can write the following code in your test case to get the desired results:
let myTable = app.tables.matching(identifier: "myUniqueTableViewIdentifier")
let cell = myTable.cells.element(matching: .cell, identifier: "myCell_0")
cell.tap()
Note that this will simply help you find and tap the required cell, you can add some XCTAssert checks inside your test case to make it more useful.
P.S.: There are many guides online which can help you learn the UI testing with Xcode. e.g.: https://blog.metova.com/guide-xcode-ui-test/
First set cell accessibilityIdentifier:
cell.accessibilityIdentifier = "Cell_\(indexPath.row)"
Get cell by identifier, then tap cell:
let cell = app.cells.element(matching: .cell, identifier: "Cell_0")
cell.tap()
Objective-C version of Kushal's answer. Last step is more simplified, though.
A- Where you define your table (usually in the viewDidLoad
of its viewController), define its accessibilityIdentifier
:
_myTable.accessibilityIdentifier = @"MyOptionsTable";
B- In cellForRowAtIndexPath
, give each cell an accessibilityIdentifier
:
cell.accessibilityIdentifier = @"MySpecialCell";
C- In your test case, get the table, then tap on the cell:
XCUIApplication *app = [[XCUIApplication alloc] init];
XCUIElement *table = app.tables[@"MyOptionsTable"];
XCUIElement *cell = table.cells[@"MySpecialCell"];
[cell tap];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With