UI测试 - 找不到与后代匹配类型的匹配...记录生成的代码

时间:2017-11-30 18:31:12

标签: ios swift xctest xcuitest

我收到错误

No matches found for Find: Descendants matching type NavigationBar from input {( Application, 0x60400019b790, pid: 20515, label: '<appname>' )}

但是代码是由XCUITest的录制功能生成的,所以我不明白它为什么找不到导航栏。我添加了accessibilityIdentifier,这是一个名为dashboardNavBar的本地化字符串,尝试使用它来查找它,但我无法查询它。我正在使用的当前代码是:

func testLoginLogout() {
    let app = XCUIApplication()
    //login credentials and other code that takes me to dashboard of app
    app.navigationBars["Dashboard"].children(matching: .button).element(boundBy: 2).tap()
    app.sheets["Do you want to Logout ?"].buttons["OK"].tap()
}

并且app.navigationBars...行是抛出上述错误的行。我希望有人可以告诉我为什么它无法找到这个导航栏或任何问题,以及我如何修复它或使用accessibilityIdentifier解决它。感谢。

1 个答案:

答案 0 :(得分:0)

我知道这可能是一种解决方法,而不是真正的解决方案,但是当我遇到此问题时我所做的事情是添加了检查元素是否存在并尝试两次运行相同操作的操作。当您在执行此操作后进行搜索时,该元素不应存在,并且不会执行第二次调用。例如:

    func testLoginLogout() {
            let app = XCUIApplication()
            if (app.navigationBars["Dashboard"].children(matching: .button).element(boundBy: 2).exists) {
                app.navigationBars["Dashboard"].children(matching: .button).element(boundBy: 2).tap()
            }
// and if you will call it again the element should not exists, otherwise it will be called again

            if (app.navigationBars["Dashboard"].children(matching: .button).element(boundBy: 2).exists) {
                app.navigationBars["Dashboard"].children(matching: .button).element(boundBy: 2).tap()
            }

            if (app.sheets["Do you want to Logout ?"].buttons["OK"].exists) {
                app.sheets["Do you want to Logout ?"].buttons["OK"].tap()
            }
        }
相关问题