Find table cell in Xcode ui testing Swift
Asked Answered
C

4

13

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

Codfish answered 8/6, 2017 at 8:58 Comment(2)
also auto recording is not working on this screen.Codfish
There is not enough information here to diagnose the problem. All we can see is that there is a table view in your view hierarchy, but you are trying to access the second cell in the table view, which may or may not be there. Please add the output of XCUIApplication().tables.cells.debugDescription so we can see what we are working with.Schach
B
28

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/

Bank answered 4/9, 2017 at 9:19 Comment(1)
This helped me a lot...Thanks!Flats
C
1

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];
Chur answered 23/5, 2020 at 19:25 Comment(0)
H
1

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()
Haemostasis answered 12/3, 2021 at 18:35 Comment(0)
H
0

If you have the cell's accessibilityIdentifier in place, the following straight-forward solution works well for me:

app.cells["your_accessibility_identifer"].tap()

The most voted answer doesn't work for me, with the table view access identifier and the table view cell identifier. It finds the table view at first, then when it comes to finding the cell it simply fails.

Source: 1.

Hydrogeology answered 15/4, 2021 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.