iOS UI Testing tap on first index of the table
Asked Answered
G

3

6

I just started studying for UI testing in iOS. When I press record and tap on first index of the table, it generate codes like this.

XCUIApplication *app = [[XCUIApplication alloc] init];
[app.tables.staticTexts[@"Apr 04 16:28"] tap];

It is good if all my data are constant. But those text will be changed from time to time. How can I modify these code so that it will always tap on the first index of the table?

Generalize answered 12/4, 2016 at 7:35 Comment(0)
C
25

Use -elementBoundByIndex on your app's cells.

XCUIApplication *app = [[XCUIApplication alloc] init];
[[app.cells elementBoundByIndex: 0] tap];
Coexist answered 13/4, 2016 at 16:12 Comment(2)
Sweet. Short and hit target. Thank :)Generalize
In Swift 4, Used "app.tables.cells.element(boundBy: 0).tap()" instead the "[[app.cells elementBoundByIndex: 0] tap];"Mossy
Y
5

Swift

If you're doing UI Testing, this will be helpful:

let cellCount = app.tables.cells.count
XCTAssertTrue(cellCount > 0)

let firstCell = app.tables.cells.element(boundBy: 0)
XCTAssertTrue(firstCell.exists)
firstCell.tap()

To answer your question though, you only need these 2 lines:

let firstCell = app.tables.cells.element(boundBy: 0)
firstCell.tap()
Yokefellow answered 13/5, 2019 at 14:30 Comment(0)
I
1

Swift 4

@Joe Masilotti's solution didn't work for me. So I used:

let app = XCUIApplication()
app.tables["table's accessibilityIdentifier"].cells.allElementsBoundByIndex.first?.tap()

"table's accessibilityIdentifier" should be replaced by your table's accessibilityIdentifier.

Possibly this will save a few minutes for someone.

Inlay answered 25/6, 2018 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.