Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 won't work for other cases. It is not universal way. May I somehow set it accessibilityIdentifier or something else?

like image 302
Bartłomiej Semańczyk Avatar asked Jul 26 '16 16:07

Bartłomiej Semańczyk


3 Answers

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

app.navigationBars.buttons.element(boundBy: 0).tap()
like image 84
Luke Avatar answered Nov 18 '22 20:11

Luke


Based on Aaron Sofaers comment, you can also set the accessibilityIdentifier directly in Interface Builder.

enter image description here

like image 26
d4Rk Avatar answered Nov 18 '22 20:11

d4Rk


Here's how you can do it in Swift 3:

app.navigationBars.buttons.element(boundBy: 0).tap()
like image 12
dustinrwh Avatar answered Nov 18 '22 22:11

dustinrwh