iphonexcode-ui-testingswift4.1xctestcase

"Neither element nor any descendant has keyboard focus" when running XCTestCase in a real iPhone


I'm trying to run a UI test case where there are two input fields exists. Following is my code

let usernameTextField = app.webViews.otherElements["Identity Server"].textFields["Username"]
let passwordField = app.webViews.otherElements["Identity Server"].secureTextFields["Password"]

_ = usernameTextField.waitForExistence(timeout: 8)

usernameTextField.tap()
usernameTextField.typeText("TEST")  // Breakpoint line

app.typeText("\n")

passwordField.tap()
passwordField.typeText("test")

When I run the test case normally it is failing with the error given in the question title. But if I add a breakpoint to the commented line, it will run without any error.

I tried to use following code snippets after the breakpoint line, separately.

sleep(8)
_ = passwordField.waitForExistence(timeout: 8)

But non of those work. As for further information, this is a Auth process scenario where those input fields resides in a web view.


Solution

  • I decided to answer myself rather than closing the question. I'll tell what went wrong in my code. The main mistake I have done was continueAfterFailure set as true. Because of that, the error shown in the wrong line not the actual error throwing line.

    So the solution is,

    continueAfterFailure = false
    

    and

    usernameTextField.tap()
    sleep(2)
    usernameTextField.typeText("TEST")
    

    There should be a small waiting time till keyboard appears in the web view before type text.