This is my case:
let passwordSecureTextField = app.secureTextFields[\"password\"]
passwordSecureTextField.tap()
passwordSecureTextField.typeText(\"wrong_pass
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.
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
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
Finally, I wrote a script which edits the Simulator's .plist file and sets the ConnectHardwareKeyboard
property to false for the selected simulator. You heard it right, it changes the property for the specifically selected simulator inside "DevicePreferences" dictionary rather than editing the global property.
First, create a shell script named disable-hardware-keyboard.sh with the following contents. You can place it within "YourProject/xyzUITests/Scripts/".:
echo "Script: Set ConnectHardwareKeyboard to false for given Simulator UDID"
if [[ $1 != *-*-*-*-* ]]; then
echo "Pass device udid as first argument."
exit 1
else
DEVICE_ID=$1
fi
DEVICE_PREFERENCES_VALUE='<dict><key>ConnectHardwareKeyboard</key><false/></dict>'
killall Simulator # kill restart the simulator to make the plist changes picked up
defaults write com.apple.iphonesimulator DevicePreferences -dict-add $DEVICE_ID $DEVICE_PREFERENCES_VALUE
open -a Simulator # IMPORTANT
Now follow these steps to call it with passing the selected simulator's udid as an argument:
Script inside Test>Pre-actions:
#!/bin/sh
# $PROJECT_DIR is path to your source project. This is provided when we select "Provide build settings from" to "AppTarget"
# $TARGET_DEVICE_IDENTIFIER is the UDID of the selected simulator
sh $PROJECT_DIR/xyzUITests/Scripts/disable-hardware-keyboard.sh $TARGET_DEVICE_IDENTIFIER
# In order to see output of above script, append following with it:
# | tee ~/Desktop/ui-test-scheme-prescript.txt
Time to test it:
[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.