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?
[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.
Another answer, but for us the problem was the view was too close to another view that a Gesture recognizer on it. We found we needed the view to be at least 20 pixels away (in our case below). Literally 15 didn't work and 20 or more did. This is strange I'll admit, but we had some UITextViews that were working and some that were not and all were under the same parent and identical other positioning (and variable names of course). The keyboard on or off or whatever made no difference. Accessibility showed the fields. We restarted our computers. We did clean builds. Fresh source checkouts.
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:
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.
Record case however you want, keyboard or without keyboard attached. But do the following before playing test.
This following option (connect hardware keyboard) should be unchecked while playing test.
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