16

Is there a way to send the application to background? Similarly to how you can call XCUIApplication.terminate(), I have some UI Elements to test on applicationDidBecomeActive(_:). Does anyone know if this is at all possible?

tilo
  • 14,009
  • 6
  • 68
  • 85
Liam
  • 2,659
  • 1
  • 14
  • 16

4 Answers4

10

I would recommend checking out XCUIDevice. Here is how you might press the home button and then relaunch the application

func testExample() {

    // Has a nav bar.
    XCTAssert(XCUIApplication().navigationBars.element.exists)

    XCUIDevice().press(XCUIDeviceButton.home)
    // Before Swift 3: XCUIDevice().pressButton(XCUIDeviceButton.Home)
    XCUIApplication().launch()

    // Navigationbar still there on second launch.
    XCTAssert(XCUIApplication().navigationBars.element.exists)
}
MonsieurDart
  • 6,005
  • 2
  • 43
  • 45
JMFR
  • 799
  • 5
  • 18
  • Works well with iOS Simulator (9.3) and Xcode 7.3. Thanks! – Vish May 13 '16 at 04:16
  • 6
    According to the API documentation of [`XCUIApplication.launch()`](http://masilotti.com/xctest-documentation/Classes/XCUIApplication.html#//api/name/launch) (Xcode 7.3.1), it TERMINATE the application if it is already running. I think it is not suitable for this case - send the application to background and then bring it up again. – Jeremy Kao May 17 '16 at 09:37
7

In my case I wanted to background the app and open it where I previously left it. To background the app:

XCUIDevice.shared.press(.home)

To re-open the app where I left it:

XCUIApplication().activate()

Re-launching the app using XCUIApplication().launch() would launch the app from new.

I am using Swift 4.2

a.ajwani
  • 868
  • 1
  • 8
  • 21
5

I just tried UIApplication.sharedApplication().performSelector("suspend") successfully.

dispatch_after(2, dispatch_get_main_queue(), {       
    // suspend the app after two seconds
    UIApplication.sharedApplication().performSelector("suspend")
})

// Swift 4 version
UIApplication.shared.perform(Selector("suspend"))
Laszlo
  • 2,803
  • 2
  • 28
  • 33
tilo
  • 14,009
  • 6
  • 68
  • 85
  • I am trying to get this to work for XCode Automated UI Tests, should this work? Or is this answer appropriate for another type of testing? – Liam Nov 11 '15 at 16:00
  • @tilo It doesn't work for me (Xcode 7.3.1). dispatch_after(2, dispatch_get_main_queue(), { // suspend the app after two seconds UIApplication.sharedApplication().performSelector(#selector(NSURLSessionTask.suspend)) }) Does it simply put the app into suspend state, instead of background state? – Jeremy Kao May 18 '16 at 03:47
  • @Liam Have you found the way to do this in UITest? I still finding on this, but no clue :( – Confused Jan 31 '17 at 20:29
5

In Xcode 9.0, Apple introduced XCUIApplication.activate(). activate() will launch the application if necessary, but will not terminate it if it is already running. Thus:

func testExample() {
    // Background the app
    XCUIDevice().press(.home)
    // Reactivate the app
    XCUIApplication().activate()
}
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
ravron
  • 11,014
  • 2
  • 39
  • 66