Xcode Swift UI Test - Simulator hardware keyboard
Asked Answered
C

2

13

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?

Count answered 31/5, 2016 at 18:28 Comment(1)
Ever found a solution?Centreboard
C
6

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
Count answered 2/6, 2016 at 12:23 Comment(1)
This changes the room ConnectHardwareKeyboard but not what's in the DevicePreferences so in my case the keyboard still stays connected.Extent
P
3

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
Porfirioporgy answered 27/4, 2020 at 21:11 Comment(1)
I'm trying to make it work with Xcode 12.x.x now and this doesn't work. The simulator still has the keyboard connected. I'm trying to set it to disconnected in order to run some UITests. Any idea?Chirp

© 2022 - 2024 — McMap. All rights reserved.