How to write XCUI test for whether I navigate to correct screen or not?

Viewed 3796

I am writing test UI test case for following UI enter image description here

I want to test on Login click whether I am navigating correctly on Dashboard screen or not.

Is there any method to do this?

My current testing code is like

func testExample() {

        let usernameTextField = app.textFields["Username"]
        usernameTextField.tap()
        usernameTextField.typeText("abc@gmail.com")

        let passwordTextField = app.textFields["Password"]
        passwordTextField.tap()
        passwordTextField.typeText("abc123")

        app.buttons["Login" ].tap()

                //let loginButton = app.staticTexts["Login"]
                //XCTAssertEqual(loginButton.exists, true)

        app.navigationBars["UIView"].buttons["Back"].tap()


    }
3 Answers

UI Tests can become really fragile when depending on text values. What I encourage you to do is to set the Accessibility Identifier for your ViewController's view. That way, even if you change the title or change the whole layout, you can still be sure you're in the correct Page/Screen/View.

class DashVC: UIViewController {
    override func viewDidLoad() {
        view.accessibilityIdentifier = "view_dashboard"
    }
}



    func test_login_withValidInput_goesDashBoard() {
        let app = XCUIApplication()

        //...    
        app.buttons["Login" ].tap()

        let dashBoardView = app.otherElements["view_dashboard"]
        let dashBoardShown = dashBoardView.waitForExistence(timeout: 5)

        XCTAssert(dashBoardShown)

    }
Related