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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
cakes88
  • 1,857
  • 5
  • 24
  • 33

6 Answers6

19

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.

ggruen
  • 1,980
  • 1
  • 15
  • 14
Mazen A
  • 206
  • 1
  • 2
12

Swift 4:

XCUIDevice.shared.press(.home)
ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
3

In Swift version 4.0.2 and 4.0.3:

XCUIDevice.shared().press(XCUIDeviceButton.home)
Davide Patti
  • 3,391
  • 2
  • 18
  • 20
2

Objective C

    [XCUIDevice.sharedDevice pressButton: XCUIDeviceButtonHome];

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

jasondinh
  • 918
  • 7
  • 21
1

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)

Yohst
  • 1,671
  • 18
  • 37
1

Now in Swift 4.1:

XCUIDevice.shared.press(XCUIDevice.Button.home)
Steve Moser
  • 7,647
  • 5
  • 55
  • 94