Ui测试失败 - 找不到“CREATE PIN”导航栏的匹配项

时间:2016-02-10 03:36:51

标签: swift xcode-ui-testing

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

此片段的目的是检查是否正在显示某个窗口。如果窗口在那里,那么我需要创建一个密码。如果不是......继续。这在窗口存在时有效。但是当窗口不存在时,我收到此错误:

Ui测试失败 - 找不到“创建PIN”导航栏

的匹配项

我已经尝试过将其重新写入此

如果XCUIApplication()。navigationBars [“CREATE PIN”] .stytyTexts [“CREATE PIN”]。label ==“CREATE PIN”{fooBar}

得到了相同的结果。我想要做的就是检查一个窗口是否在那里,如果采取行动......如果它不采取另一个。

Swift UI测试

1 个答案:

答案 0 :(得分:1)

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

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

因此,您应该检查父级的现有(navBar)。 PS。此外,您还可以阅读有关期望和可访问性标识符的信息。