带有 Auth0 的 XCTestCase:如何解除安全警报“XXXX"想要使用“auth0.com"登录 [英] XCTestCase with Auth0: How to dismiss security alert “XXXX” Wants to Use “auth0.com” to Sign In

查看:27
本文介绍了带有 Auth0 的 XCTestCase:如何解除安全警报“XXXX"想要使用“auth0.com"登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以最近苹果推出了这个提示:XXXX"想使用auth0.com"登录其中XXXX"是 ios 应用名称.

So recently Apple introduced this prompt: "XXXX" Wants to Use "auth0.com" to Sign In Where "XXXX" is the ios app name.

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

This alert/dialog comes up when in the case of Auth0 the user clicks on "Login with Google" or "Login with Facebook". That’s all nice but when running IOS UI tests, this dialog doesn’t go away when using the usual way of dismissing system dialogs:

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 中关闭此对话框?

It kinda makes sense to me: This is a security system dialog introduced by Apple in order to improve security. Having it easily dismissed in the code would defeat the purpose.
But still, anyone knows if it's possible to dismiss this dialog in an XCTestCase?

推荐答案

我认为 Apple 希望开发人员能够利用引入的 addUIInterruptionMonitor.

I think Apple expects from a developer to make use of the introduced addUIInterruptionMonitor.

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

In fact, the addUIInterruptionMonitor(withDescription: )is not working, so I went down the road to access the Springboard and select the appropriate permission on the system alert.

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)
}

可选:跳板课程

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

I also created a Springboard Class, as I have also System Settings tests etc. running...

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

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

This way, you could call your XCUITestCase extension the following way:

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

<小时>

如果 addUIInterruptionMonitor(withDescription: ) 确实有效,则它可能如下所示:


If, the addUIInterruptionMonitor(withDescription: ) was actually working, this could then look like the following:

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

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)

这篇关于带有 Auth0 的 XCTestCase:如何解除安全警报“XXXX"想要使用“auth0.com"登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆