iosswiftxcode-ui-testing

Programmatically sending an app to background


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?


Solution

  • 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)
    }