Ui 测试失败 - 未找到 "CREATE PIN" 导航栏的匹配项

Ui Testing failure - No matches found for "CREATE PIN" Navigationbar

var pcheck = XCUIApplication().navigationBars["CREATE PIN"].staticTexts["CREATE PIN"].label

  if pCheck == "CREATE PIN" {
    app.childrenMatchingType(.Window).elementBoundByIndex(0).childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).elementBoundByIndex(1).childrenMatchingType(.SecureTextField).element.typeText("1111")

    app.childrenMatchingType(.Window).elementBoundByIndex(0).childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).element.childrenMatchingType(.Other).elementBoundByIndex(1).childrenMatchingType(.SecureTextField).element.typeText("1111")
    } else {
        print("No Pin needed")
    }

此代码段的目的是检查是否显示了某个 window。如果 window 在那里,那么我需要创建一个密码。如果没有……继续。这在 window 存在时起作用。但是,当 window 不存在时,我收到此错误:

Ui 测试失败 - 未找到 "CREATE PIN" 导航栏

的匹配项

我试过重写这个

if XCUIApplication().navigationBars[[=​​25=]].staticTexts["CREATE PIN"].label == "CREATE PIN" {fooBar}

得到了同样的结果。我想做的就是检查 window 是否存在,如果它正在采取行动...如果它没有采取另一个行动。

Swift UI 测试

您尝试从不存在的父级获取 staticText 元素。

let someElement = XCUIApplication().navigationBars["CREATE PIN"]
if (someElement.exists){  //also it may be someElement.hittable  
//do something 
}

因此,您应该检查父项(导航栏)是否存在。 PS。阅读有关期望和可访问性标识符的内容也会对您有所帮助。