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

3

7

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


    }
Hardej answered 13/8, 2017 at 6:33 Comment(0)
L
5

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)

    }
Leonoreleonsis answered 27/10, 2017 at 12:36 Comment(1)
I think this is not sufficient answer/explination. I would like to suggest you, modify your answer with proper steps also if you can share the screenshot it will be greatSmack
L
2

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
Lipp answered 8/9, 2017 at 10:53 Comment(0)
P
0

Try adding an accessibility indicator to the back button so you can check for availability using backButton.exists or backButton.hittable and assert accordingly. In any case, if you set

continueAfterFailure = false

in setUp(), your test will fail as it is if it doesn’t find a button with “Back”.

Prepositor answered 13/8, 2017 at 7:22 Comment(5)
Thanks for answer..but i want test case like...if i navigate to any screen just want to test if I am on correct screen or not without interacting with that screens elements...because back button can be available on screen on which i don't want to navigateHardej
That was the reason for adding the accessibility identifier, so each element is uniquely identifiablePrepositor
Do i need to add accessibility identifier programatically because in my UI it is created automatically as i am using navigation controller ..so i am not able to find way to put accessibility identifier in StoryboardHardej
You can add it programmatically. I would add it to another distinct element though (instead of the backButton). Programatically you can just do yourObject.accessibilityIdentifier = "YOUR_UNIQUE_ID"Prepositor
You could also do this for any element on the dashboard. The convention is to find a UI element unique to that screen and check it exists, so when you add components to your dashboard, pick one and check it exists so you don't have to rely on customising the back button.Embrue

© 2022 - 2024 — McMap. All rights reserved.