Tomáš Linhart
Tomáš Linhart

Reputation: 14299

How do I check a cell is visible in a table view with Xcode 7 UI Testing?

I have a table view with a lot of cells and not every cell is visible on a screen. When I check with

table.cells.staticTexts.matchingIdentifier("My Cell").element.exists

It returns true but the cell is not visible on the screen and I cannot tap on it. Because whenever I tap on it, a test fails.

How to check if an element is visible on a screen? Or how to tap on an element that is not visible?

Upvotes: 8

Views: 4372

Answers (4)

toxicsun
toxicsun

Reputation: 397

you can use swipeUp method to scroll down until specific cell will be visible. You can also check that cell exist or not. something like this.

XCUIElementQuery *tableQuery = app.tables;
 if (!tablesQuery.cells.staticText[@"some text"].exist){
  [tablesQuery.staticTexts[@"visible cell text"] swipeUp];
}

Upvotes: 0

gnasher729
gnasher729

Reputation: 52530

The tableview method cellForRowAtIndexPath (NOT the delegate method of the same name) will return the cell at some index path if it is currently being displayed, or nil if it is not displayed.

If the user clicks on a button (or if anything happens with any view in your cell), you can also go up the view hierarchy to first find the cell, then the table view, and the method indexPathForCell will give you the index path of the cell.

Upvotes: 0

Oletha
Oletha

Reputation: 7639

Use the hittable property instead of exists.

The class reference for XCUIElement explains that the hittable property will only return true if the element can be touched.

table.cells.staticTexts.matchingIdentifier("My Cell").element.hittable

Upvotes: 9

Naveen Kumar S R
Naveen Kumar S R

Reputation: 129

Instead of using element.exists try using element.hittable. This has worked for me, exists returns true if the element is currently in display hierarchy even though it is outside the screen view. hittable returns true only if the element is on screen and its hittable.

Upvotes: 0

Related Questions