Home » Ios » UI Testing Failure – Neither element nor any descendant has keyboard focus on secureTextField

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

This is my case:

let passwordSecureTextField = app.secureTextFields["password"]
passwordSecureTextField.tap()
passwordSecureTextField.typeText("wrong_password") //here is an error

UI Testing Failure – Neither element nor any descendant has keyboard focus. Element:

What is wrong? This is working nice for normal textFields, but problem arise only with secureTextFields. Any workarounds?

Answers:

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

Questions:
Answers:

Recently we found a hack to make solution from accepted answer persistent. To disable Simulator setting: ‘Hardware -> 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.

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

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

Questions:
Answers:

Use a sleep between launching the app and typing in data in textfields like this:

sleep(2)

In my case I was keeping getting this error every time and only this solution helped my out.

Questions:
Answers:

This maybe help: I just add a “tap” action before the error; that´s all 🙂

[app.textFields[@"theTitle"] tap];
[app.textFields[@"theTitle"] typeText:@"kk"];

Questions:
Answers:
func pasteTextFieldText(app:XCUIApplication, element:XCUIElement, value:String, clearText:Bool) {
    // Get the password into the pasteboard buffer
    UIPasteboard.generalPasteboard().string = value

    // Bring up the popup menu on the password field
    element.tap()

    if clearText {
        element.buttons["Clear text"].tap()
    }

    element.doubleTap()

    // Tap the Paste button to input the password
    app.menuItems["Paste"].tap()
}

Questions:
Answers:

Record case however you want, i mean with both keyboard or without keyboard attached.
But do the following when playing test.

enter image description here

This option (connect hardware keyboard) should be unchecked while playing test.

Questions:
Answers:
[Reposting Bartłomiej Semańczyk‘s comment as an answer because it solved the problem for me]

I needed to do Simulator > Reset Contents and Settings in the simulator menu bar to make this start working for me.

Questions:
Answers:

Your first line is just a query definition, which doesn’t mean that passwordSecureTextField would actually exist.

Your second line will dynamically execute the query and try to (re)bind the query to UI element. You should put a breakpoint on it and verify that one and only one element is found. Or just use an assert:

XCTAssertFalse(passwordSecureTextField.exists);

Otherwise it looks ok, tap should force keyboard visible and then typeText should just work. Error log should tell you more info.

Questions:
Answers:

Don’t messed up, There problem caught the reason is you are recorded your testing time your app will connection hardware keyboard while your automatic testing time simulator takes only software keyboard. so for how to fix this issues. Just use software keyboard on your recording time. you can see the magic.

Questions:
Answers:

The problem for me was the same as for Ted. Actually if password field gets tapped after login field and hardware KB is on, software keyboard will dismiss itself on second field tap, and it’s not specific to UI tests.

After some time messing around with AppleScript, here’s what I came up with(improvements are welcome):


tell application "Simulator"
activate
tell application "System Events"
try
tell process "Simulator"
tell menu bar 1
tell menu bar item "Hardware"
tell menu "Hardware"
tell menu item "Keyboard"
tell menu "Keyboard"
set menuItem to menu item "Connect Hardware Keyboard"
tell menu item "Connect Hardware Keyboard"
set checkboxStatus to value of attribute "AXMenuItemMarkChar" of menuItem
if checkboxStatus is equal to "✓" then
click
end if
end tell
end tell
end tell
end tell
end tell
end tell
end tell
on error
tell application "System Preferences"
activate
set securityPane to pane id "com.apple.preference.security"
tell securityPane to reveal anchor "Privacy_Accessibility"
display dialog "Xcode needs Universal access to disable hardware keyboard during tests(otherwise tests may fail because of focus issues)"
end tell
end try
end tell
end tell

Create a script file with code above and add it to necessary targets(probably UI tests target only, you may want to add similar script to your development targets to re-enable HW keyboard during development).
You should add Run Script phase in build phases and use it like this:
osascript Path/To/Script/script_name.applescript

Questions:
Answers:

We encountered the same error when setting the accessibilityIdentifier value for a custom view (UIStackView subclass) containing UIControl subviews. In that case XCTest was unable to get the keyboard focus for the descendent elements.

Our solution was simply to remove the accessibilityIdentifier from our parent view and set the accessibilityIdentifier for the subviews through dedicated properties.

Questions:
Answers:

Had the same issue with Securetextfields. The connect hardware option in my Simulator was of, but still ran into the issue. Finally, this worked for me (Swift 3):

 let enterPasswordSecureTextField = app.secureTextFields["Enter Password"]
    enterPasswordSecureTextField.tap()
    enterPasswordSecureTextField.typeText("12345678")