Swift 在Xcode 7 UI自动化中模拟按下Home按钮

Swift 在Xcode 7 UI自动化中模拟按下Home按钮,swift,ios9,ui-automation,xcode7,ios-ui-automation,Swift,Ios9,Ui Automation,Xcode7,Ios Ui Automation,我把它归结为: XCUIDevice.pressButton(noideawhatgoeshere) 我试过XuiDeviceButtonHome,home,home,1 如何在iOS上模拟按Xcode中的home按钮?我不知道如何模拟home按钮-这可能不可能。您可以使用以下命令对其进行排序:[[UIApplication sharedApplication]terminate]或者您可以使用以下命令将应用程序置于后台指定的持续时间:UIATarget.localTarget()。停用eap

我把它归结为:

XCUIDevice.pressButton(noideawhatgoeshere)
我试过XuiDeviceButtonHome,home,home,1


如何在iOS上模拟按Xcode中的home按钮?

我不知道如何模拟home按钮-这可能不可能。您可以使用以下命令对其进行排序:
[[UIApplication sharedApplication]terminate]
或者您可以使用以下命令将应用程序置于后台指定的持续时间:
UIATarget.localTarget()。停用eappforduration(秒)


(使用objc的借口)

您需要先获取设备实例。因此,要模拟按下home(主页)按钮:

XCUIDevice.sharedDevice().pressButton(XCUIDeviceButton.Home)
应该可以工作(我在物理设备上也可以)

谢谢

玛赞

Swift 5版本:

XCUIDevice.shared.press(XCUIDevice.Button.home)
此外,还验证了它在模拟器中工作,至少在运行iPadOS 13.2.2的模拟“iPad Pro(9.7英寸)”的Xcode 11.2.1中工作。

    [XCUIDevice.sharedDevice pressButton: XCUIDeviceButtonHome];

还有用于音量增大按钮的
xguiDeviceButtonVolumeUp
,以及用于音量减小按钮的
xguiDeviceButtonVolumeDown

Swift 4

XCUIDevice.shared.press(.home)
XCUIDevice.shared().press(XCUIDeviceButton.home)
XCUIDevice.shared.press(XCUIDevice.Button.home)

在Swift版本4.0.24.0.3中:

XCUIDevice.shared.press(.home)
XCUIDevice.shared().press(XCUIDeviceButton.home)
XCUIDevice.shared.press(XCUIDevice.Button.home)

现在在Swift4.1中:

XCUIDevice.shared.press(.home)
XCUIDevice.shared().press(XCUIDeviceButton.home)
XCUIDevice.shared.press(XCUIDevice.Button.home)

有人知道如何在测试之间双击home(主页)按钮以终止应用程序的任务吗?Swift 3:
xguidevice.shared()。按(.home)