Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Swift UI Test - Simulator hardware keyboard

I'm automating UI testing using swift in xcode and I need the hardware keyboard to always be disabled for my tests. Is there a way to make sure the hardware keyboard is disabled without human intervention, i.e. a command line script? These UI tests will be run on a build server, so manually launching a simulator and turning off the hardware keyboard is undesirable.

I've looked at xcrun simctl options to interact with the simulator, but I haven't been able to figure out how to do what I want to do. Also, I found a few SO posts indicating that what I'm trying to do is not possible, but I wasn't sure if the posts were asking exactly what I was asking.

Can I set hardware keyboard to off for iOS simulators through a shell script?

like image 376
Alex Avatar asked May 31 '16 18:05

Alex


2 Answers

There is a plist with simulator preferences located at ~/Library/Preferences

To change it to turn off the hardware keyboard, make sure the simulator is off, then run this command:

defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool no

You can use xcrun simctl options in a script to shut down simulators.

EDIT

Apple changed this sometime in 2018-2019, I found this answer somewhere else on SO, it works for me:

/usr/libexec/PlistBuddy -c "Print :DevicePreferences" ~/Library/Preferences/com.apple.iphonesimulator.plist | perl -lne 'print $1 if /^    (\S*) =/' | while read -r a; do /usr/libexec/PlistBuddy -c "Set :DevicePreferences:$a:ConnectHardwareKeyboard false" ~/Library/Preferences/com.apple.iphonesimulator.plist || /usr/libexec/PlistBuddy -c  "Add :DevicePreferences:$a:ConnectHardwareKeyboard bool false" ~/Library/Preferences/com.apple.iphonesimulator.plist; done
like image 136
Alex Avatar answered Sep 19 '22 15:09

Alex


For newer versions of Xcode including XCODE 11. This works nicely. Add it to the pre-actions of the UITest target as a run script:

xcrun simctl shutdown ${TARGET_DEVICE_IDENTIFIER}
plutil -replace DevicePreferences.${TARGET_DEVICE_IDENTIFIER}.ConnectHardwareKeyboard -bool NO ~/Library/Preferences/com.apple.iphonesimulator.plist
like image 36
knappsimon Avatar answered Sep 21 '22 15:09

knappsimon