以编程方式将应用发送到后台

以编程方式将应用发送到后台

问题描述:

是否可以将应用程序发送到后台?与如何调用XCUIApplication.terminate()相似,我有一些UI元素可以在applicationDidBecomeActive(_:)上进行测试.有人知道这是否完全可能吗?

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?

我建议签出XCUIDevice.这是您可以按主页"按钮然后重新启动应用程序的方式

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