user2263712
user2263712

Reputation: 21

XCUITest cannot detect the accessibility id of my dynamically generated tableView

I am writing UI tests using XCUIApplication. I am testing for selected Index on a cell row. When I run the tests I am unable to get the text of the selected index. However I am able to click on the selected row but using the static text of the bottom tableView. This happens when there is a tableViewController underneath. I am trying to add a ViewController with a tableView on top of another TableViewController. The tableViewController underneath is generated using storyboard but the above viewController is dynamically generated.

Setup: I am using an xib for the row for the tableView and i am using a custom UITableViewCell with a label and and image. Both Label and image have accessibility turned on but not the cell. I have tried turning on accessibility on the cell and still didn't work.

This is my test

//the BoxListTable is my ViewController that is on top
// I added the following code in that viewController:
   self.view.isAccessibilityElement = true
   self.view.accessibilityIdentifier = "BoxListTable"

//I also added these for the tableView but it doesn't show up in my tests
   tableView.accessibilityIdentifier = "tableview"
   tableView.isAccessibilityElement = true

//my test is the following

 XCUIApplication().tables["BoxListTable"].tap()

when I run this test the test click on the full tableView but cannot get any of the static texts. It also cannot get any cells or the tableview. po XCUIApplication.tables["BoxListTable"].cells.count it return 0. //I tried
XCUIApplication.tables["BoxListTable"].element.children(matching: .other)
return 0

Can you please let me know why I cannot see the accessibilityIdentifier even though it is set.

Upvotes: 2

Views: 6060

Answers (1)

dzoanb
dzoanb

Reputation: 341

First of all, if you turn on isAccessibilityElement flag on parent view (in your case it is the view of BoxListTable) all of its children won't be accessible. The same applies to UITableView and UITableViewCell. But you can set their accessibilityIdentifiers and they will be visible for all the UI element, even those with isAccessibilityElement evaluating to false.

So there is this pure XCTest version but it will work only for cells already loaded (all of the lazily loaded cells won't be available) and may be obstructed by keyboard or navigation bar. Also if you look for content of element without performing any action on it framework itself won't scroll to this element. So checking if view exists will pass but isHittable will fail as the element may not be out of an area user can see.

let app = XCIUApplication()
let selectedCell: XCUIElement = table.cells.element(matching: NSPredicate(format: "isSelected == true"))
selectedCell.tap() // If you want to make sure framework will scroll to it.
selectedCell.staticTexts["yourText"]
// or
selectedCell.staticTexts.element(boundBy: index)

I would encourage you to try using AutoMate library that can save you all above troubles.

let app = XCIUApplication()
let selectedCell: XCUIElement = table.cells.element(matching: NSPredicate(format: "isSelected == true"))
XCTAssertFalse(selectedCell.isVisible) // `isVisible` checks if element exists in hierarchy and is visible to the user.
table.swipe(to: .down, untilVisible: selectedCell, from: app)
XCTAssertTrue(selectedCell.isVisible)

To see how all of this methods work, please, check the example app I've made >>here<<.

If you find any problems with isSelected property on XCUIElement you should check my answer to >>this question<<. As you can see in the example app I had this problem (Xcode Version 8.3.2 (8E2002)).

Upvotes: 7

Related Questions