UITests: app resume from background thanks to Siri

This is a quick post about a new feature added by Apple in Xcode8.3 that let me test an edge case through UITests.

The problem

Let’s consider to have a business logic only applied when the app is resumed from background. And let’s say that if the app is resumed from background then your app navigation changes due to your business logic.

Until XCode8.2 every time a new UITest is executed, the app process is killed and the app is executed from scratch.

Browsing the Apple doc I’ve noticed that they have added the access to Siri through XCUIDevice and the only method exposed by it is:

XCUIDevice.shared().siriService.activate(voiceRecognitionText: "")

This let me to simulate the flow in which the user puts the app in background (using the Home button) and then resumes the app using Siri.

The code

I’ve created this sample project on github in which I’ve implemented a really smart logic:

1. if the app is executed from scratch and the user clicks on the button --> show an amazing popup

https://player.vimeo.com/video/218925200

2. if the app is resumed from background and the user clicks on the button --> push a view controller

https://player.vimeo.com/video/218925135

Well basically these two cases are summarized in this little tiny code:

--> Test#1 <--
func testMainNavigationFromScratch() {
  let app = XCUIApplication()
app.buttons["mainBtnAccId"].tap()
app.alerts["test"].buttons["oK"].tap()
}
--> Test#2: resuming from background <--
func testMainNavigationFromBackground() {

XCUIDevice.shared().press(XCUIDeviceButton.home)
let appName = // get name from bundle
  XCUIDevice.shared().siriService.activate(voiceRecognitionText: "open \(appName) app")

sleep(3)
  self.checkUI() // some UITest code
}

That’s it! With Xcode 8.3 you can enrich your UITests using Siri, for instance testing custom actions sent from it to your app!


Checkout the github repo about this post and feel free to contact me on Twitter for any reason. I’d love to hear any kind of feedback! ✌🏼

Thanks for reading! 👽


PS: Here you can find the complete list of what a user can ask to Siri.