Search code examples
iosswiftxcodexcode-ui-testing

UI testing a tab bar controller


I have built a simple tab bar with 3 tabs.

enter image description here

I want to run a UI test to make sure that if the user clicks a tab bar item, the correct view controller shows. How would I go about doing that? Below is the code I would start with, just don't know how to write my assertion.

func testTabBarMyProfileButton() {
    let tabBarsQuery = XCUIApplication().tabBars
    tabBarsQuery.buttons["My Profile"].tap()
}

func testTabBarGraphsButton() {
    let tabBarsQuery = XCUIApplication().tabBars
    tabBarsQuery.buttons["Graphs"].tap()
}

func testTabBarAboutButton() {
    let tabBarsQuery = XCUIApplication().tabBars
    tabBarsQuery.buttons["About"].tap()
}

Solution

  • 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.