9

I am using XCUITest to test UI behaviour when the keyboard is present - for example, if content properly moves up when the keyboard appears.

For some reason, the iOS simulator insists on regularly just disabling the software keyboard. Sometimes while the simulator is restarted, but sometimes even just when switching textfields in the middle of a test.

Is there a way to always force the software keyboard in the simulator? e.g. a command line argument or a property on XCUIDevice? I am running these tests on a CI, so manually enabling the software keyboard in the simulator is not an option.

BlackWolf
  • 5,239
  • 5
  • 33
  • 60

3 Answers3

16

I added a prescript in Xcode. It writes defaults ConnectHardwareKeyboard NO to ~/Library/Preferences/com.apple.iphonesimulator.plist file AND quits the Simulator. Quitting simulator is important because when the tests relaunch the simulator, only then it takes into account the defaults value we wrote.

enter image description here

killall Simulator
defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool false
Marián Černý
  • 15,096
  • 4
  • 70
  • 83
Hasaan Ali
  • 1,192
  • 16
  • 22
4
external_kb_connected=false

osascript -e 'quit app "Simulator"'

SIMUS_KEYBOARD=$(/usr/libexec/PlistBuddy -c "Print :DevicePreferences" ~/Library/Preferences/com.apple.iphonesimulator.plist | perl -lne 'print $1 if /^    (\S*) =/')

echo "$SIMUS_KEYBOARD" | while read -r a; do /usr/libexec/PlistBuddy -c "Set :DevicePreferences:$a:ConnectHardwareKeyboard $external_kb_connected" ~/Library/Preferences/com.apple.iphonesimulator.plist || /usr/libexec/PlistBuddy -c  "Add :DevicePreferences:$a:ConnectHardwareKeyboard bool $external_kb_connected" ~/Library/Preferences/com.apple.iphonesimulator.plist; done

Running the above script in a "Build phase" of the test target will disable the external keyboard in all simulators.

Nick
  • 3,958
  • 4
  • 32
  • 47
NormalGuy
  • 97
  • 14
-2

I had the same issue and this worked for me:

(Click on Simulator) > Hardware > Keyboard

then uncheck "Connect Hardware Keyboard"

Community
  • 1
  • 1
Len_X
  • 825
  • 11
  • 29
  • 4
    Thanks for the response. I should have mentioned that the tests are run on a CI, so any manual fix for this is unfortunately not possible. Still voted the answer up since it might help somebody who is having this issue on their local machine. – BlackWolf Mar 27 '19 at 20:34
  • 1
    I think it is implied in the question that the test is automated - thus, no manual clicking on the Simulator menu to enable software keyboard. – Jacob M. Barnard Oct 09 '19 at 14:22