Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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("[email protected]")

        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()


    }
like image 978
New iOS Dev Avatar asked Aug 13 '17 06:08

New iOS Dev


2 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)

    }
like image 159
Daniel Carlos Avatar answered Sep 20 '22 17:09

Daniel Carlos


Try this

    app.buttons["Login - Login"].tap()
    XCTAssertEqual(app.navigationBars.element.identifier, "appname.CalculationView") //If your second view controller is SecondViewController, your identifier is appname.SecondView.Like that my second view controller is CalculationViewController so my identifier is CalculationView
like image 38
divya d Avatar answered Sep 20 '22 17:09

divya d