XCUITest - How do I type into a secure text field? (password)

I get the error "Neither Element nor its descendant has Keyboard focus"

It looks like I'm using it on the wrong UIElement.

Any help will be great!


Thanks.

  • I am also facing this problem

    https://thewing.pk/

Add a Comment

Replies

Ran into the same issue. Here is a workaround:



let app = XCUIApplication()


// put the password on the clipboard

UIPasteboard.generalPasteboard().string = "the password" // substitute "the password" with the actual password

app.secureTextFields["passwordTextField"].doubleTap() // substitute "PasswordTextField" with appropriate identifier

app.menuItems["Paste"].tap()

I got the same error and thought it was due to the secure field, too, but I've seen it happen with non-secure UITextFields too, especially after tapping one field, entering text, then tapping another field.


Anyway, try disconnecting your hardware keyboard from the simulator, especially if you notice that the simulator keyboard is not visible when this failure occurs. Shortcut is CMD + SHIFT + K, menu option in the simulator is at Hardware > Keyboard > Connect Hardware Keyboard.


Note that this prevents you from typing in the simulator on your physical keyboard.

The simplest solution is to just call tap on the elment to gain focus before doing typeText.

What you say is true, however, it only works for text fields and not secure text fields.

Please make sure that you file bug reports when you encounter issues like this one (using the Report Bugs link at the bottom of this page). You might all be providing some important bit of information that we would need to recognize what you're running into. Thanks!

Hi Joar,


I have filed a bug for this issue.

Thanks!

Thank you - this helped me.


I actually had to slightly adjust this due to working in a non-English language app. The problem was that the identifier of the Paste button changes according to language, so instead I'm just making it tap on the first (only) button in the popup menu:


UIPasteboard.generalPasteboard().string = "the password"
passwordField.doubleTap()
app.menuItems.elementBoundByIndex(0).tap()


I wrote this up and a couple of other tips here: https://rnorth.org/11/automated-ui-testing-in-xcode-7

Hi vsh,


Was there any response to the bug you filed about this in September? I'm still running into the same problem you describe at present.

Yep and in Xcode 9.1 / 9.2.2.

Have filed a bug rdar:/35648209

The issue is related to the hardware keyboard: it should be disabled before launching UI tests. For this, add a new run script action in pre-actions for Test by editing your .xcscheme.

The command line is: defaults write com.apple.iphonesimulator ConnectHardwareKeyboard 0

After your update, your .xcscheme must be:

...
 <TestAction
      ...>
      <PreActions>
         <ExecutionAction
            ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
            <ActionContent
               title = "Disable hardware keyboard (required to tap text when switching text fields)"
               scriptText = "defaults write com.apple.iphonesimulator ConnectHardwareKeyboard 0&#10;">
            </ActionContent>
         </ExecutionAction>
      </PreActions>
      ...

Note: it will be only effective after restarting your simulator.

I recently ran into this issue that I had to solve in a different way. I tried disabling the hardware keyboard, but that didn't help.

It turned out that I had a hidden TextField right next to the SecureTextField that was somehow interfering with the click.

The text field was hidden using the code below. I didn't get too far into debugging to understand why this may have caused issues, but I was able to work around it by hiding the field using a conditional.

I hope this is helpful to anyone who runs into this issue in the future!

extension View {
    func hidden(_ isHidden: Bool) -> some View {
        ModifiedContent(content: self, modifier: Show(hidden: isHidden))
    }
}

struct Show: ViewModifier {
    let hidden: Bool

    @ViewBuilder
    func body(content: Content) -> some View {
        if hidden {
            content.hidden()
        } else {
            content
        }
    }
}
Add a Comment

test