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

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

问题描述

因此,最近Apple引入了此提示:"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希望开发人员利用引入的

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天全站免登陆