Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find table cell in Xcode ui testing Swift

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.The events table view which I need to find in my ui testing

like image 577
Qandil Tariq Avatar asked Jun 08 '17 08:06

Qandil Tariq


3 Answers

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/

like image 190
Kushal Ashok Avatar answered Oct 11 '22 17:10

Kushal Ashok


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()
like image 41
Umair Ali Avatar answered Oct 11 '22 16:10

Umair Ali


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];
like image 1
CSawy Avatar answered Oct 11 '22 16:10

CSawy