UI Testing iOS, Selecting Secure Text Entry textField
Asked Answered
J

3

17

I have password and password conf textFields that I have applied the "Secure Text Entry", to mask the password. However, when I run the UI tests, those fields can not be found getting

UI Testing Failure - No matches found for "password" TextField

I'm attempting to select the fields like so:

let passwordTextField = app.textFields["password"]

but then it fails when I try to tap:

passwordTextField.tap()

Any ideas on how I can access the field?

Jdavie answered 23/10, 2015 at 16:7 Comment(5)
What's the failure message?Spinet
The failure specifically is "UI Testing Failure - No matches found for "password" TextField". But when I unselect "Secure Text Entry" it finds it no problemJdavie
Can you open the Accessibility Inspector and see what the text field's identifier is? It might not be password when it becomes a sure entry field.Spinet
Also, can you try using secureTextFields instead of textFields?Spinet
Just rechecked, the Accessibility Inspector and the identifier is "password".Jdavie
S
47

As discussed in the comments, when accessing a secure text field use the secureTextFields selector.

let passwordTextField = app.secureTextFields["password"]
passwordTextField.tap()
passwordTextField.typeText("my secure password")
Spinet answered 23/10, 2015 at 16:52 Comment(1)
Wow. I've looked at several articles regarding this UI Testing and this is the first time I've heard of the secureTextFields selector. Thanks!Invocate
C
8

Also if you need to type to secureTextField

If menu I/O Hardware Keyboard is ON typeText not working for secureTextField.

enter image description here

Use below to type to secure field

        password.tap()

        app.keys["t"].tap()
        app.keys["e"].tap()
        app.keys["s"].tap()
        app.keys["t"].tap()
Conto answered 20/5, 2020 at 9:54 Comment(0)
H
0

Joe's answer is good, but it was insufficient for my use case. I had to find all text fields by index, no matter if it's a normal text field or a secure text field. I couldn't find any information on the web about this.

That's what I came up with:

// elementType must be specified as integer
//
// See:
//  * https://developer.apple.com/documentation/xctest/xcuielementtype/xcuielementtypetextfield
//  * https://developer.apple.com/documentation/xctest/xcuielementtype/xcuielementtypesecuretextfield
let textFieldPredicate = NSPredicate(format: "elementType == 49")
let secureTextFieldPredicate = NSPredicate(format: "elementType == 50")
let predicate = NSCompoundPredicate(
    orPredicateWithSubpredicates: [
        textFieldPredicate,
        secureTextFieldPredicate
    ]
)

let textFields = app.descendants(matching: .any).matching(predicate)
let textField = textFields.firstMatch
textField.tap()
textField.typeText('yeah boi')
Hydroxy answered 21/10, 2022 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.