How to access back bar button item in universal way under UITests in Xcode?

Bartłomiej Semańczyk picture Bartłomiej Semańczyk · Jul 26, 2016 · Viewed 12.2k times · Source

The problem is when I try to access back bar button item because it is presented with:

  • Restaurants title,
  • Back title
  • without title

like it is on the screens:

enter image description here enter image description here enter image description here

Currently I access it like this:

let backButton = XCUIApplication().buttons["Restaurants"]

but it wont work for other cases. It is not universal way. May I somehow set it accessibilityIdentifier or sth else?

Answer

Luke picture Luke · Jul 26, 2016

Generally the back button tends to be the first button element in the navigation bar

app.navigationBars.buttons.element(boundBy: 0).tap()