3

Im working on with XCUITest, where in its a simple screen, having just username textField, password textField and a Login Button. I could able assert like on tap of Login button, both username textField and Password textField is non empty. But what to do something like, i want to typeText in username and password textFields. Which is possible on first field only as im getting error with below code.

func testUserNameShouldNotBeEmptyOnLoginButtonTapped() {

        app.launch()

        let loginButton = app.buttons["Login"]

        let userNameField = app.textFields["UsernameTextField"]
        userNameField.tap()
        userNameField.typeText("Dummy User")
        sleep(2)

        let passwordField = app.textFields["PasswordTextField"]
        passwordField.tap()
        passwordField.typeText("Yohooooooso")

        loginButton.tap()

        XCTAssertTrue(userNameField.exists)
        guard let userNameValue = userNameField.value as? String,
        let passwordValue = passwordField.value as? String else {
            XCTAssert(true, "UsernameTextFieldUsernameTextFieldUsernameTextField")
            return
        }
        if passwordValue.isEmpty {
            XCTAssert(false, "Password Cannot be Empty")
        } else if userNameValue.isEmpty {
            XCTAssert(false, "UserName Cannot be empty")
        } else {
            XCTAssert(true, "Both Fields are valid")
        }
    }

How can i able to change the focus of Keyboard to select field or i hide keyboard on done with the typeText on first field.

Im getting the exception on the passwordTextField as below

Neither element nor any descendant has keyboard focus. Event dispatch snapshot: TextField, identifier: 'PasswordTextField'
Mahesh S
  • 309
  • 1
  • 11
  • I am facing the same issue, I tried adding a test prescript which sets the ConnectHardwareKeyboard to false in the com.apple.iphonesimulator.plist but the problem is still there. – Hasaan Ali Jun 05 '20 at 13:36

1 Answers1

0

My first thought is that your password textfield is hidden by the keyboard when you try to tap it to get focus. You should either close the keyboard or scroll a bit to make the textfield visible.

You also can use the isHittable property to be sure that elements can be hit.

cesarmarch
  • 617
  • 4
  • 13