Simulating pressing the Home button in Xcode 7 UI Automation
Asked Answered
M

5

9

I've got it down to:

XCUIDevice.pressButton(noideawhatgoeshere)

I've tried XCUIDeviceButtonHome, home, Home, 1

How do I simulate pressing the home button in Xcode on iOS?

Madaih answered 29/9, 2015 at 21:10 Comment(0)
L
20

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

Swift 5 Version:

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

Also, verified it works in the simulator, at least in Xcode 11.2.1 on a simulated "iPad Pro (9.7-inch)" running iPadOS 13.2.2.

Lineate answered 12/10, 2015 at 17:47 Comment(3)
Anyone know of a way to double-press the home button to be able to task-kill the app between tests?Cranio
Swift 3: XCUIDevice.shared().press(.home)Brunette
Does something needs to be imported in order to use XCUIDevice (tried to import XCUIDevice)? I'm getting 'Cannot find 'XCUIDevice' in scope'.Flail
S
12

Swift 4:

XCUIDevice.shared.press(.home)
Sibelius answered 21/10, 2017 at 22:44 Comment(0)
L
3

In Swift version 4.0.2 and 4.0.3:

XCUIDevice.shared().press(XCUIDeviceButton.home)
Lesh answered 13/12, 2017 at 15:11 Comment(0)
A
2

Objective C

    [XCUIDevice.sharedDevice pressButton: XCUIDeviceButtonHome];

There are also XCUIDeviceButtonVolumeUp for volume up button and XCUIDeviceButtonVolumeDown for volume down button.

Axillary answered 14/10, 2015 at 22:53 Comment(0)
C
1

Now in Swift 4.1:

XCUIDevice.shared.press(XCUIDevice.Button.home)
Culet answered 22/3, 2018 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.