typeText() is failing on physical iOS device

I have an XCTest case which is trying to enter text into a text field.


let app = XCUIApplication()
let query = app.tables

query.staticTexts[“New..."].tap()
       
let serverAddress = query.cells.containing(.cell, identifier:"serverTableViewCell").textFields.element
if serverAddress.exists {
    serverAddress.tap()
    serverAddress.typeText(server)
}


This has to run on a physical device, because the application is a Network Extension VPN.


It generally works fine, but every now and then it starts failing. When it fails everything looks like it's succeeding.

  • I can find the field
  • I can tap on the field, and I see the text cursor
  • I can call typeText()

No errors, messages, informative output... But nothing appears in the text field.


This appears to happen after there's been some sort of system dialog on the device (not entirely sure it's related, but the correlation is strong) but even once that dialog is gone the test continues to fail.


Things are complicated because the device is in the office--and we aren't allowed to go into the office at the moment. I can't re-start the device, because I'm pretty sure that will require physical presence at the device to unlock it again.


I *can* alter settings on the device using teh XCTest interfaces to control System Settings. I tried disabling all notifications, but things are still failing.


I saw a similar question with the same symptoms, but it was on a simulator, and the solution was to reset the simulator. Not an option in this case: https://forums.developer.apple.com/thread/125010


Is it possible that once the system dialog/alert/notification is apparently gone that it's still hogging keyboard input? If that is the case, anyone know how I could clear it? Without being physically present at the device?


Any other suggestions would be great.

I've seen something similar when trying to manipulate the app after clearing a dialog. We use the following code to handle typing in such cases:


extension XCUIElement {
    func tapAndWaitForKeyboardToAppear() {
        let waitTime = 0.5
        let retryMaxCount = 20
        let keyboard = XCUIApplication().keyboards.element
        for _ in 0..< retryMaxCount {
          tap()
            if keyboard.waitForExistence(timeout: waitTime) {
                return
            }
        }
        XCTAssert(false, "keyboard failed to appear")
    }
}


In our tests, we'll call


        element.tapAndWaitForKeyboardToAppear()
        element.typeText("this is some text")


Hope this helps.

No, the keyboard is definitely visible when it's failing. Oddly I've found that I can call the keys directly and the text does show up, so:


app.keys["a"].tap()


enters an 'a' and


app.keyboards.buttons["shift"].tap()


hits the shift key.


This means that I can probably work around it by essentially re-implementing typeText() with my own individual key strokes...


But what a pain. The built-in function failing says that there's *something* wrong on the device and I don't know how that could impact other tests.

typeText() is failing on physical iOS device
 
 
Q