Daniel Creagh
Daniel Creagh

Reputation: 502

Determining if an app has quit using XCTest UI tests

A certain function in my iOS Swift app will quit the app if the build is deemed to be invalid (out of date etc.).

How do I use XCTest to determine if the app has successfully quit and has returned the simulator to the homescreen?

I've tried the obvious ways like

XCTAssertFalse(XCUIApplication().exists, "app should have quit")

and

XCTAssertFalse(XCUIApplication().enabled, "app should have quit")

if I try and do any operations on XCUIApplication it hangs.

Upvotes: 2

Views: 394

Answers (1)

barndog
barndog

Reputation: 7173

You can register an observer in the NSNotificationCenter for UIApplicationDidEnterBackgroundNotification and in your tests, if the build is found to be out of date, make a test exception:

let expectation = expectationWithDescription("myDescription")

waitForExpectationsWithTimeout(10) { error in
}

and in your notification handler:

func someHandler() {
    expectation.fulfill()
    //unregister the observer
}

That way if the notification is posted, the expectation will be fulfilled, otherwise the test will fail. Be sure to only set up this case if the build is outdated, otherwise it will always fail if the notification isn't posted.

Upvotes: 1

Related Questions