Cucumber XCUItest系统警报

Cucumber XCUItest系统警报,cucumber,xctest,xctestcase,xcuitest,Cucumber,Xctest,Xctestcase,Xcuitest,我正在尝试使用XCUITest和Cucumberish自动化我的应用程序,我无法单击系统警报,如位置和联系人权限,但无法单击“允许”或“确定”,而是随意单击“不允许”或“允许” 这是我试图在步骤定义中使用的代码: systemAlertMonitorToken = addUIInterruptionMonitor(withDescription: "Location Dialog") { (alert) -> Bool in if alert.buttons.matchin

我正在尝试使用XCUITest和Cucumberish自动化我的应用程序,我无法单击系统警报,如位置和联系人权限,但无法单击“允许”或“确定”,而是随意单击“不允许”或“允许”

这是我试图在步骤定义中使用的代码:

systemAlertMonitorToken = addUIInterruptionMonitor(withDescription: "Location Dialog") { (alert) -> Bool in
        if alert.buttons.matching(identifier: "Allow").count > 0 {
            alert.buttons["Allow"].tap()
            return true
        }
        else if alert.buttons.matching(identifier: "OK").count > 0{
            alert.buttons["OK"].tap()
            return true
        }
        else {
            return false
        }
    }

我使用此代码在我的应用程序中点击权限屏幕:

 let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")

 let allowBtn = springboard.buttons[identifier]

 if allowBtn.waitForExistence(timeout: 4) {
      allowBtn.tap()
 }

其中[标识符]将是“允许”或“不允许”或您选择的任何选项。

我使用此代码在我的应用程序中点击权限屏幕:

 let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")

 let allowBtn = springboard.buttons[identifier]

 if allowBtn.waitForExistence(timeout: 4) {
      allowBtn.tap()
 }

其中[identifier]将是“允许”或“不允许”或您选择的任何选项。

我怀疑使用匹配(标识符:“允许”)有问题。计数。也许Xcode认为“不允许”和“允许”按钮是同一个XUIElement

如果您知道“允许”和“确定”按钮将始终位于同一个boundBy位置,您可以按该位置点击按钮。我只需检查按钮是否存在,然后根据按钮的boundBy编号点击按钮

例如,如果警报有两个按钮,左侧按钮显示“允许”,右侧按钮显示“不允许”,则代码如下所示:

systemAlertMonitorToken = addUIInterruptionMonitor(withDescription: "Location Dialog") { (alert) -> Bool in
    if alert.buttons["Allow"].exists || alert.buttons["OK"].exists {
        alert.buttons.element(boundBy: 1).tap()
        return true
    }
    else {
        return false
    }
}

我怀疑使用匹配(标识符:“允许”).count有问题。也许Xcode认为“不允许”和“允许”按钮是同一个XUIElement

如果您知道“允许”和“确定”按钮将始终位于同一个boundBy位置,您可以按该位置点击按钮。我只需检查按钮是否存在,然后根据按钮的boundBy编号点击按钮

例如,如果警报有两个按钮,左侧按钮显示“允许”,右侧按钮显示“不允许”,则代码如下所示:

systemAlertMonitorToken = addUIInterruptionMonitor(withDescription: "Location Dialog") { (alert) -> Bool in
    if alert.buttons["Allow"].exists || alert.buttons["OK"].exists {
        alert.buttons.element(boundBy: 1).tap()
        return true
    }
    else {
        return false
    }
}

目前很难区分什么是按钮标签以及什么是问题中的功能的一部分。能否使用标签周围的引号来更好地解释?在上述情况下,屏幕截图可能有助于更好地理解问题。尝试对if语句使用与if语句中相同的查询(使用
[“Allow”]
而不是
匹配(标识符:)
)目前很难区分什么是按钮标签以及什么是问题中的功能的一部分。能否使用标签周围的引号来更好地解释?在上述情况下,屏幕截图可能有助于更好地理解问题。尝试对if语句使用与if语句中相同的查询(使用
[“Allow”]
而不是
匹配(标识符:)