【问题标题】:Xcode Swift UI Test - Simulator hardware keyboardXcode Swift UI 测试 - 模拟器硬件键盘
【发布时间】:2016-05-31 18:28:56
【问题描述】:

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

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

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

【问题讨论】:

  • 找到解决方案了吗?

标签: ios xcode ios-simulator xcode-ui-testing


【解决方案1】:

~/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 $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

【讨论】:

  • 这会改变房间 ConnectHardwareKeyboard,但不会改变 DevicePreferences 中的内容,所以在我的情况下,键盘仍然保持连接状态。
【解决方案2】:

对于更新版本的 Xcode,包括 XCODE 11。这很好用。将其作为运行脚本添加到 UITest 目标的 pre-actions:

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

【讨论】:

  • 我现在正试图让它与 Xcode 12.x.x 一起工作,但这不起作用。模拟器仍然连接着键盘。我正在尝试将其设置为断开连接以运行一些 UITests。有什么想法吗?
猜你喜欢
  • 2019-04-13
  • 2016-03-10
  • 2012-10-27
  • 2016-01-05
  • 2014-11-13
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多