iosswiftxcuitest

XCUITests: how can I tap "back to app" button appearing after testing app opens some other app?


My app has a feature that allows me to open system settings (Preferences app). The system Back button appears in such cases, with a left arrow and the original app name.

I mean this one:

enter image description here

So, my question is: how can I tap this button in XCUITests? I tried to use Springboard app:

let app = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let button = app.buttons["my-app"]
if button.exists {
    button.tap()
}

And Preferences app:

let app = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
let button = app.buttons["my-app"]
if button.exists {
    button.tap()
}

But there was no result. What app shows the button? With what identifier can I find the button?


Solution

  • Rather than working with the system UI, which has a tendency to change out from under you and break tests, I'd suggest simply calling activate() on your app, which should bring it to the foreground.

    If you really feel the need to use that back button, just know you're testing the OS, not your application.