XCUITest sign in with Apple

235 Views Asked by At

I'm trying to automate a testing flow in my app, that requires signing in with Apple first. I've figured out how open settings and login with a test Apple ID. I've wrapped that up in a function and call that first (with a check to see if it's already logged in).

When I return to my app, and tapping the sign in with Apple button, I can't find anyway to interact with the sign in with Apple modal, e.g. asking me if I want to share my email address or not, tapping "continue with password" and so on.

I've tried setting up an interruption handler, but it doesn't get triggered.

let test = addUIInterruptionMonitor(withDescription: "") { alert in
            
    print("inside alert:\( alert )")
            
    return true
}

I've tried checking for springboard alerts, but it's not an alert:

let springboardApp = XCUIApplication(bundleIdentifier: "com.apple.springboard")
springboardApp.alerts.exists

I don't know any other tricks, or how to find the bundle ID of this popup. Any advice?

1

There are 1 best solutions below

0
Simon McLoughlin On

Managed to find the bundle identifier needed com.apple.AuthKitUIService

This is not a complete solution that handles all paths through the modal. Just a sample to get through the flow when you have previously signed in. Just leaving this here to show how to wrap something like this up in a function

sleep(4)
let testApp = XCUIApplication(bundleIdentifier: "com.apple.AuthKitUIService")
        
let continueButton = testApp.buttons["Continue"]
if continueButton.exists {
    continueButton.tap()
}
        
let shareEmailOption = testApp.tables.staticTexts["Share My Email"]
if shareEmailOption.exists {
    shareEmailOption.tap()
}
        
let continueWithPassword = testApp.buttons["Continue with Password"]
if continueWithPassword.exists {
    continueWithPassword.tap()
}
        
testApp.secureTextFields["Password"].tap()

// Type your password

testApp.buttons["Sign In"].tap()

// Wait for something to show up in your app to denote success / failure
let app = XCUIApplication()
.......

How I found this bundle identifier for anyone having similar issues:

running xcrun simctl list devices --json in terminal lists all simulators. Find the UDID of the one you are using then either:

run xcrun simctl listapps <UDID>

or go to ~/Library/Developer/CoreSimulator/<UDID>/data/Containers/Data and do a search for "com.apple"

Note:

The above code requires, at least, having logged into an apple id on the simulator. This can be handled with the below, supplying your own credentials

public static func handleLoggingInToAppleIdIfNeeded() {
    let settingsApp = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
    settingsApp.launch()
        
    sleep(2)
    if settingsApp.staticTexts[<your user display name>].exists {
        return
    }
        
        
    settingsApp.staticTexts["Sign in to your iPhone"].tap()
    settingsApp.textFields["Email"].tap()
    // Type you email
        
    settingsApp.buttons["Next"].tap()
    sleep(4)
        
    settingsApp.secureTextFields["Required"].tap()
    // Type your password
        
        
    settingsApp.buttons["Next"].tap()
    sleep(4)
        
    settingsApp.buttons["Don't Merge"].tap()
        
    SharedHelpers.shared.waitForButton("Sign Out", exists: true, inApp: settingsApp, delay: 4)
}

followed by a call to wake up your own app

let app = XCUIApplication()
app.launch()
sleep(2)