Xcode Swift UI 测试 - 模拟器硬件键盘

Xcode Swift UI Test - Simulator hardware keyboard

我正在使用 xcode 中的 swift 自动执行 UI 测试,我需要在测试时始终禁用硬件键盘。有没有办法确保在没有人为干预的情况下禁用硬件键盘,即命令行脚本?这些 UI 测试将在构建服务器上 运行,因此手动启动模拟器并关闭硬件键盘是不可取的。

我查看了与模拟器交互的 xc运行 simctl 选项,但我一直无法弄清楚如何做我想做的事情。此外,我发现一些 SO 帖子表明我正在尝试做的事情是不可能的,但我不确定这些帖子是否问的正是我在问的问题。

我可以通过 shell 脚本将 iOS 模拟器的硬件键盘设置为关闭吗?

~/Library/Preferences

处有一个带有模拟器首选项的 plist

要将其更改为关闭硬件键盘,请确保模拟器已关闭,然后运行此命令:

defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool no

您可以在脚本中使用 xcrun simctl 选项来关闭模拟器。

编辑

Apple 在 2018-2019 的某个时候更改了这个,我在 SO 的其他地方找到了这个答案,它对我有用:

/usr/libexec/PlistBuddy -c "Print :DevicePreferences" ~/Library/Preferences/com.apple.iphonesimulator.plist | perl -lne 'print  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

对于 Xcode 的较新版本,包括 XCODE 11。这很好用。将其作为 运行 脚本添加到 UITest 目标的预操作中:

xcrun simctl shutdown ${TARGET_DEVICE_IDENTIFIER}
plutil -replace DevicePreferences.${TARGET_DEVICE_IDENTIFIER}.ConnectHardwareKeyboard -bool NO ~/Library/Preferences/com.apple.iphonesimulator.plist