简体   繁体   English

XCUITest 的 iOS 模拟器中的强制软件键盘

[英]Force Software Keyboard in iOS Simulator for XCUITest

I am using XCUITest to test UI behaviour when the keyboard is present - for example, if content properly moves up when the keyboard appears.我正在使用 XCUITest 来测试存在键盘时的 UI 行为 - 例如,当键盘出现时内容是否正确向上移动。

For some reason, the iOS simulator insists on regularly just disabling the software keyboard.出于某种原因,iOS 模拟器坚持定期禁用软件键盘。 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?有没有办法在模拟器中始终强制使用软键盘? eg a command line argument or a property on XCUIDevice?例如命令行参数或 XCUIDevice 上的属性? I am running these tests on a CI, so manually enabling the software keyboard in the simulator is not an option.我在 CI 上运行这些测试,因此无法在模拟器中手动启用软件键盘。

I added a prescript in Xcode.我在 Xcode 中添加了一个处方。 It writes defaults ConnectHardwareKeyboard NO to ~/Library/Preferences/com.apple.iphonesimulator.plist file AND quits the Simulator.它将默认 ConnectHardwareKeyboard NO 写入 ~/Library/Preferences/com.apple.iphonesimulator.plist 文件并退出模拟器。 Quitting simulator is important because when the tests relaunch the simulator, only then it takes into account the defaults value we wrote.退出模拟器很重要,因为当测试重新启动模拟器时,它才会考虑我们编写的默认值。

在此处输入图片说明

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.在测试目标的“构建阶段”中运行上述脚本将禁用所有模拟器中的外部键盘。

I had the same issue and this worked for me:我有同样的问题,这对我有用:

(Click on Simulator) > Hardware > Keyboard (点击模拟器)> 硬件> 键盘

then uncheck "Connect Hardware Keyboard"然后取消选中“连接硬件键盘”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM