How do I target in-app purchase drawer element with XCUITEST?

205 views Asked by At

I am trying to write a UI test for our app's IAP using a sandbox account. However, when trying to tap this purchase button, the test is unable to find this element:screenshot of in-app-purchase prompt

I've tried targeting this element by its button name and label name with no success. During the UI recording, when tapping this button, Xcode freezes completely and I am forced to shut Xcode down. I've also tried debugging at this step and when I print out "app.debugDescription" in the console, this button nor any of the other elements in this purchase drawer is returned.

Failing line of code: app.buttons["Purchase"].tap()

Error message: No matches found for Elements matching predicate '"Purchase" IN identifiers'

Xcode Version: 13.2.1 iOS Version tested (real iPhone device): 15.3.1

2

There are 2 answers

0
ed2311 On BEST ANSWER

Through trial and error, I've discovered that you can target the purchase button through the SpringBoard.

let springBoard =  XCUIApplication(bundleIdentifier: "com.apple.springboard")
springBoard.buttons["Purchase"] // A match will return
2
Mike Collins On

Previously, Apple has not permitted any testing of IAP at all. Recently they introduced testing to the StoreKit framework, but it's at the unit testing level; I wouldn't be surprised if they're programmatically blocking access to automation for security reasons.

If this were to work you'd need to find the bundle identifier of the application that's "running" (the StoreKit window) and interact with that. I unfortunately don't have an application available (one with a purchase available) to allow me to look into this for you.

A forced tap on the coordinates (I always add a few pixels to the x and y for reliability) should work. Never ideal, but might be your only hope.

Another option would be to set the auto-accept alerts capability? It might work. capabilities.SetCapability("autoDismissAlerts", true)