8

所以最近苹果推出了这个提示:“XXXX” Wants to Use “auth0.com” to Sign In 其中“XXXX”是ios应用名称。

在此处输入图像描述

在 Auth0 的情况下,当用户单击“使用 Google 登录”或“使用 Facebook 登录”时,会出现此警报/对话框。这一切都很好,但是在运行 IOS UI 测试时,当使用通常的方式关闭系统对话框时,此对话框不会消失:

func doUserLogin(_ app: XCUIApplication) {

    app.staticTexts["notLoggedInActivelabel"].tap()
    // this will bring up oauth0 login window in ios

    // setup a handler to dismiss the system alert
    let handler = self.addUIInterruptionMonitor (withDescription: "allow oauth") { (alert) -> Bool in
        // code should come here where the dialog is presented, 
        // but it never does ....   
        alert.buttons["Continue"].tap() // click Continue Button 
        return true
    }

    // click the login with GOOGLE button. This brings up dialog “XXXX” Wants to Use “auth0.com” to Login
    app.scrollViews.otherElements.buttons["LOG IN WITH GOOGLE"].tap()

    // this step is required when using addUIInterruptionMonitor
    app.tap()

    removeUIInterruptionMonitor(handler)
}

这对我来说有点道理:这是 Apple 为提高安全性而引入的安全系统对话框。让它在代码中很容易被忽略会破坏目的。
但是,任何人都知道是否可以在 XCTestCase 中关闭此对话框?

4

1 回答 1

6

我认为 Apple 期望开发人员使用引入的addUIInterruptionMonitor

事实上,addUIInterruptionMonitor(withDescription: )它不起作用,所以我继续访问 Springboard 并在系统警报上选择适当的权限。

1. 扩展 XCTestCase 以重用此功能,如有必要

extension XCTestCase {

    // I hope this code is mostly reusable
    // I didn't test it for Location Permission While in Use vs. Always...
    func setPermission(for alert:XCUIElement, allow: Bool) -> Bool {
        if alert.elementType == .alert {

            // make sure to support any language
            // Might also be "allow" for some dialogs
            let buttonIdentifier = allow ? "Continue" : "Cancel"
            let identifierButton = alert.buttons[buttonIdentifier]
            if identifierButton.exists && identifierButton.isHittable {
                identifierButton.tap()
                return true
            }

            // Or, if you don't want to bother with the language/identifiers
            // Allow = Last button Index (probably 1), Cancel = 0
            let buttonIndex = allow ? alert.buttons.count - 1 : 0
            let indexButton = alert.buttons.element(boundBy: buttonIndex)
            if indexButton.exists && indexButton.isHittable {
                indexButton.tap()
                return true
            }
        }
        return false
    }
}

2.在你的测试中调用这个函数,比如

// This holds a reference to your SignIn/Login XCUIElement
yourSignInButton.tap()

let systemAlerts = XCUIApplication(bundleIdentifier: "com.apple.springboard").alerts
if systemAlerts.count > 0 {
    _ = self.setPermission(for: systemAlerts.element(boundBy: 0), allow: true)
}

可选:跳板类

我还创建了一个跳板类,因为我还运行了系统设置测试等......

class Springboard {
    static let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
}

这样,您可以XCUITestCase通过以下方式调用您的扩展程序:

let systemAlerts = Springboard.springboard.alerts
if systemAlerts.count > 0 {
    self.setPermission(for: systemAlerts.element(boundBy: 0), allow: true)
}

如果addUIInterruptionMonitor(withDescription: )实际工作,则可能如下所示:

注意:目前,仅适用于位置、麦克风等的授权/权限警报。

let interruptionMonitor = addUIInterruptionMonitor(withDescription: "Allow the app and website to share information") { (alert) -> Bool in
    return self.setPermission(for: systemAlerts.element(boundBy: 0), allow: true)
}

// This holds a reference to your SignIn/Login XCUIElement
yourSignInButton.tap()

removeUIInterruptionMonitor(interruptionMonitor)
于 2018-11-13T09:11:21.753 回答