XCTest UI Testing - How to close and open an app without relaunch?
Asked Answered
D

4

12

I want to make my app to go to background, and then comeback to the foreground.

To make an app to go background: XCUIDevice.shared().press(XCUIDeviceButton.home)

To terminate an app(force tap): XCUIApplication().terminate()

To launch the app: XCUIApplication().launch()

Problem: when I try to close and open the app, the launch() method clears the app from background and it opens the app freshly.

I saw this comment regarding this. But cant able to figure out it in UI test. I'm using Swift. Help needed!!

Dorindadorine answered 31/1, 2017 at 7:5 Comment(0)
W
22

As of Xcode 9 and iOS 11, XCUIApplication() has an activate() method that you can use to relaunch the app.

As brandenbyers suggested, you can "press" the home button to background your app, and then activate it again like this to avoid using Siri:

XCUIDevice.shared.press(.home)
XCUIApplication().activate()

Note that this only works with targets built using XCUITest, not XCTest. If you try this within a target built from XCTest, all XCUIApplication operations will crash.

Westmorland answered 24/10, 2017 at 6:52 Comment(3)
This is the solutionMadly
Does this work with a device that has no home button, such as an iPhone X?Flivver
@Flivver Yes this does work for any device no matter the style of the physical button.Sustentation
U
5

As of Xcode 8.3 and iOS 10.3, it is now possible to relaunch your backgrounded app with Siri!

XCUIDevice.shared().press(XCUIDeviceButton.home)
XCUIDevice.shared().siriService.activate(voiceRecognitionText: "Open {appName}")

Be sure to include @available(iOS 10.3, *) at the top of your test suite file.

Uteutensil answered 27/3, 2017 at 23:4 Comment(2)
Siri always shows the following error in simulator: "Sorry, I'm having trouble with the connection. Please try agin in a moment"Dorindadorine
I have only used this with physical devices. Are you getting the same error from Siri when using a real device?Uteutensil
M
1

I have follow what @randenbyers mentioned on simulator. Before that I have manually activated Siri Service.

XCUIDevice.shared().press(XCUIDeviceButton.home)
if #available(iOS 10.3, *) {
    XCUIDevice.shared().siriService.activate(voiceRecognitionText: "Open {Writer}")
    XCTAssertTrue(XCUIApplication().tabBars.buttons["My Projects"].exists)
} else {
    app.scrollViews.otherElements.buttons["Log Out"].tap()
    assertionFailure("Fail because Siri service is not activated")
}
Mitziemitzl answered 14/7, 2017 at 8:16 Comment(0)
C
1

A similar question was asked and answered here Possible to bring the app from background to foreground?

This is what I have in my XCUITest and it works like a charm (xcode 10.1 and test device is iPhone X 11.0)

func testWhatever() {

// You test steps go here until you need the background foreground to run
// To background the app 
XCUIDevice.shared.press(XCUIDevice.Button.home) 
// To bring the app back
XCUIApplication().activate() 

// You test continues after background foreground has been done. }
Cripple answered 29/5, 2019 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.