Simulating Pressing the Home button in xCode 7 UI

2019-06-21 06:29发布

I've got it down to:

XCUIDevice.pressButton(noideawhatgoeshere)

I've tried XCUIDeviceButtonHome, home, Home, 1

Not sure how to simulate pressing the home button, does anyone have any pointers? I'm new to xCode/swift

6条回答
ら.Afraid
2楼-- · 2019-06-21 06:37

You need to get the device instance first. So to simulate pressing the home button:

XCUIDevice.sharedDevice().pressButton(XCUIDeviceButton.Home)

should work (it does for me on a physical device)

Thanks!

Mazen

查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-21 06:41

Now in Swift 4.1:

XCUIDevice.shared.press(XCUIDevice.Button.home)
查看更多
【Aperson】
4楼-- · 2019-06-21 06:48

Swift 4:

XCUIDevice.shared.press(.home)
查看更多
放荡不羁爱自由
5楼-- · 2019-06-21 06:51

I know of no way to simulate the home button - this might not be possible. You can sorta brute force it with: [[UIApplication sharedApplication] terminate]; or you can put your app in the background for a specified duration with: UIATarget.localTarget().deactivateAppForDuration(seconds);

(excuses for using objc)

查看更多
爷的心禁止访问
6楼-- · 2019-06-21 07:01

In Swift version 4.0.2 and 4.0.3:

XCUIDevice.shared().press(XCUIDeviceButton.home)
查看更多
劫难
7楼-- · 2019-06-21 07:02

Objective C

    [XCUIDevice.sharedDevice pressButton:1];

Home button = 1, Volume up button = 2, Volume down button = 3.

查看更多
登录 后发表回答