Skip to content

Instantly share code, notes, and snippets.

@raxxpack
Created February 18, 2020 20:59
Show Gist options
  • Save raxxpack/07c23346762c574e6bc85559259761ff to your computer and use it in GitHub Desktop.
Save raxxpack/07c23346762c574e6bc85559259761ff to your computer and use it in GitHub Desktop.
Confirm purchase with retry
extension XCUIApplication {
/// Confirms buying of a subscription purchase and retries once if the user is not authorized.
private func confirmBuyingOfSubscription(signInRequired: Bool = true,
account: AppStoreConnectService.AppStoreSandboxUser,
retryCount: Int,
_ block: () -> ()) {
block()
if signInRequired {
signInToiTunesSandboxAccountAndBuy(account: account)
}
let springboardApp = XCUIApplication.getSpringboardApp()
let subTermsAlert = springboardApp.alerts["Subscription Terms"].waitForIt()
subTermsAlert.buttons["Continue"].waitForIt().waitToBeHittable().tap()
let confirmSubAlert = springboardApp.alerts["Confirm Subscription"].waitForIt()
confirmSubAlert.buttons["OK"].waitForIt().waitToBeHittable().tap()
// Try to retry if Apple's sandbox environment is acting out.
let notAuthorizedAlert = springboardApp.alerts["You are not authorized to make purchases of this InApp in Sandbox at this time."]
if retryCount == 0, notAuthorizedAlert.waitForExistence(timeout: 30) {
notAuthorizedAlert.buttons["OK"].waitForIt().waitToBeHittable().tap()
// Sign out of the sandbox account.
XCUIApplication.launchSettingsApp().signOutOfiTunesSandboxAccount()
// Switch back to app and try again.
activate()
confirmBuyingOfSubscription(account: account, retryCount: retryCount + 1, block)
return
}
let subscribedAlert = springboardApp.alerts["You’re all set"].waitForIt()
subscribedAlert.buttons["OK"].waitForIt().waitToBeHittable().tap()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment