arvere
arvere

Reputation: 737

XCUIElement unique identifier

In XCUITests, is there an unique way to identify a given XCUIElement that persists across queries?

I can't use labels because sometimes elements can come with identical labels.

Hash/hashvalue works within a given query, but it is refreshed after consecutive queries.

As always, I can't find documentation on this subject :(

Upvotes: 1

Views: 2259

Answers (2)

Travis Statham
Travis Statham

Reputation: 1

The way I've done it is to add the indexPath.row to the end of a String and set that as the AccessibilityIdentifier.

self.accessibilityIdentifier = "ProfileTableViewCell-\(indexPath.row)"
self.detailLabel.accessibilityIdentifier = "ProfileTableViewCell-DetailLabel-\(indexPath.row)

I even add the row value to the internal cell elements.

Upvotes: 0

Oletha
Oletha

Reputation: 7649

You can uniquely identify XCUIElements by setting an accessibilityIdentifier on the UIView object they represent.

// app code
let button = UIButton()
button.accessibilityIdentifier = "myButton"

// test code
let app = XCUIApplication()
let button = app.buttons["myButton"]
button.tap()

Accessibility identifiers are used solely for the purpose of UI testing. They are not localised and it is your responsibility to set the accessibility identifier to something that is as unique as you require. The accessibility identifier will persist for the lifetime of the view, unless your code explicitly changes it.

Upvotes: 1

Related Questions