Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!!

like image 857
Confused Avatar asked Jan 31 '17 07:01

Confused


2 Answers

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.

like image 173
Neal Avatar answered Oct 10 '22 02:10

Neal


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. }
like image 32
Jasmeet Singh Avatar answered Oct 10 '22 02:10

Jasmeet Singh