Force Software Keyboard in iOS Simulator for XCUITest
Asked Answered
M

3

11

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.

Moise answered 27/3, 2019 at 15:58 Comment(0)
H
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
Hexose answered 30/7, 2019 at 10:21 Comment(4)
killall Simulator defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool falseTrifocal
Please someone approve the answer update to include the script.Grandfather
This wont work with Xcode project with Multiple Testplans (esp when one is Unit Testplan and another is UITestplan)Esquiline
In Xcode 14.3.1, I found when Simulator was started, it will auto-set ConnectHardwareKeyboard = true under your booted simulator's DevicePreference. This will override the settings in Pre-actions or script.Apotheosize
P
5
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.

Plusch answered 25/4, 2019 at 18:45 Comment(3)
It sure would be nice if Apple had a default to allow the hardware keyboard to be connected AND set the keyboard to be displayed. : /Pylorectomy
It might also be good to copy the current settings file in this script and add it to a Before Run script and have an After Run script to restore it. If I have time, I'll add it in another reply.Pylorectomy
Beautiful. Thank you!Nappe
C
-2

I had the same issue and this worked for me:

(Click on Simulator) > Hardware > Keyboard

then uncheck "Connect Hardware Keyboard"

Cockeye answered 27/3, 2019 at 17:8 Comment(2)
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.Moise
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.Protrusion

© 2022 - 2024 — McMap. All rights reserved.