如何使用 Swift XCUITest 添加摇晃手势? [关闭]

Posted

技术标签:

【中文标题】如何使用 Swift XCUITest 添加摇晃手势? [关闭]【英文标题】:How to add shake gesture with Swift XCUITest? [closed] 【发布时间】:2021-11-02 14:07:53 【问题描述】:

有没有办法在 Swift UI 测试中以编程方式触发摇晃手势?

【问题讨论】:

介意与我们分享您的尝试吗?您是否有想要通过手动触发来解决的问题?模拟可以解决这个问题吗? 那有什么用处? 【参考方案1】:

XCUITest 目前不支持此功能,但多年来一直是常见的请求。这不会无头工作,但是AFAIK没有办法在无头模式下运行ios模拟器(也许CI可以做到?),使用AppleScript触发菜单项是唯一的方法:

tell application "Simulator" to activate
tell application "System Events"
  keystroke "z" using control down, command down
end tell

您也可以让脚本选择特定的菜单项,而不是使用键盘快捷键,但我认为这可能更容易出错:

tell application "Simulator" to activate
tell application "System Events"
  tell process "Simulator"
    click menu item "Shake" of menu "Device" of menu bar 1
  end tell
end tell

Can you execute an Applescript script from a Swift Application 解释了如何在 Swift 中执行 AppleScript。

【讨论】:

以上是关于如何使用 Swift XCUITest 添加摇晃手势? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章