UI testing a tab bar controller

You can access the tabbar button by its position:

app.tabBars.buttons.element(boundBy: 2).tap()

If you have different controls in each view controller shown on each tab bar, you can make assertions if they exist or not (what is expected). For example if the first tab bar has UILabel named "First name" you can assert if it exists by writing

Let theLabel = app.staticTexts["myValue"]
XCTAssert(theLabel.exists).to(beTrue)

And on the other screens do the same thing for the different controls.


If anyone finds this looking to UI test the contents of another app, I just found a solution..

The tab bar item is a lazy variable and needs to be touched before you can reference a tab bar button by value. Add this line:

tabBarItem.accessibilityIdentifier = "my-snazzy-identifier"

to the viewDidLoad method and you should be able to do this in your UI tests:

app.tabBars.buttons["Button Title"].tap()