Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI Testing Failure - Neither element nor any descendant has keyboard focus on secureTextField

This issue caused me a world of pain, but I've managed to figure out a proper solution. In the Simulator, make sure I/O -> Keyboard -> Connect hardware keyboard is off.


Recently we found a hack to make solution from accepted answer persistent. To disable Simulator setting: I/O -> Keyboard -> Connect hardware keyboard from command line one should write:

defaults write com.apple.iphonesimulator ConnectHardwareKeyboard 0

It will not affect a simulator which is running - you need to restart simulator or start a new one to make that setting have its effect.


I have written a small extension (Swift) which works perfect for me. Here is the code:

extension XCTestCase {

    func tapElementAndWaitForKeyboardToAppear(element: XCUIElement) {
        let keyboard = XCUIApplication().keyboards.element
        while (true) {
            element.tap()
            if keyboard.exists {
                break;
            }
            NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.5))
        }
    }
}

The main idea is to keep tapping an element (text field) before the keyboard is presented.


Stanislav has the right idea.

In a team environment, you need something that will automatically work. I've come up with a fix here on my blog.

Basically you just paste:

UIPasteboard.generalPasteboard().string = "Their password"
let passwordSecureTextField = app.secureTextFields["password"]
passwordSecureTextField.pressForDuration(1.1)
app.menuItems["Paste"].tap()

Another cause of this error is if there is a parent view of the text field in which you are trying to enter text that is set as an accessibility element (view.isAccessibilityElement = true). In this case, XCTest is not able to get a handle on the subview to enter the text and returns the error.

UI Testing Failure - Neither element nor any descendant has keyboard focus.

It isn't that no element has focus (as you can often see the keyboard up and blinking cursor in the UITextField), it is just that no element it can reach has focus. I ran into this when attempting to enter text in a UISearchBar. The search bar itself is not the text field, when setting it as an accessibility element, access to the underlying UITextField was blocked. To resolve this, searchBar.accessibilityIdentifier = "My Identifier" was set on the UISearchBar however the isAccessibilityElement was not set to true. After this, test code of the form:

app.otherElements["My Identifier"].tap()
app.otherElements["My Identifier"].typeText("sample text")

Works


It occurred with me for many times. You have to disable Keyboard Hardware and Same Layout as OSX in your Simulator

Hardware/Keyboard (disable all)

After that keyboard software won't dismiss and your tests can type text

Disable Hardware


In my case this Hardware -> Keyboard -> Connect Hardware Keyboard -> Disable didn't worked for me.

But when I followed

1) Hardware -> Keyboard -> Connect Hardware Keyboard -> Enabled and run the app
2) Hardware -> Keyboard -> Connect Hardware Keyboard -> Disable.

It worked for me