UI测试-找不到匹配类型的后代……正在记录生成的代码

问题描述 投票:5回答:1

我遇到错误

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...行是引发以上错误的行。我希望有人能告诉我为什么它找不到此navBar或存在什么问题,以及如何使用accessibilityIdentifier修复或解决它。谢谢。

ios swift xctest xcuitest
1个回答
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()
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.