【问题标题】:XCTestCase: Can't press custom bar button in navigation barXCTestCase:无法在导航栏中按下自定义栏按钮
【发布时间】:2020-06-15 13:49:54
【问题描述】:

我正在为 iOS 应用程序编写测试,但无法单击自定义栏按钮。它是一个带有 UIImageView 的条形按钮。 该图像具有accessibilityIdentifier = "settingsBarImage" 和 条形按钮有一个accessibilityIdentifier = "settingsBarButton"

当我使用print(app.debugDescription) 打印小部件树时,按钮存在并且可以点击。

NavigationBar, 0x60000320b480, {{0.0, 44.0}, {375.0, 44.0}}, identifier: 'Overview'
     Button, 0x60000320b560, {{16.0, 51.0}, {30.0, 30.0}}, identifier: 'settingsBarButton', label: 'Your profile picture.'
     StaticText, 0x60000320b640, {{150.0, 55.7}, {75.0, 20.3}}, label: 'Overview'

我试过了:

app.navigationBars.button.firstMatch.tap()

app.navigationBars["Overview"].buttons["settingsBarButton"].tap()

app.buttons["settingsBarButton"].tap()

app.otherElement["settingsBarButton"].tap()

及其每一种组合。

结果总是一样的:

t =    14.74s Tap Button
t =    14.74s     Wait for com.sap.mobile.apps.ProjectCompanion to idle
t =    14.77s     Find the Button
t =    15.82s         Find the Button (retry 1)
t =    16.87s         Find the Button (retry 2)
t =    16.92s         Collecting extra data to assist test failure triage
t =    16.92s             Requesting snapshot of accessibility hierarchy for app with pid 89909
t =    16.95s             Requesting snapshot of accessibility hierarchy for app with pid 89909
t =    17.06s         Assertion Failure: OverviewScreenBase.swift:31: Failed to get matching snapshot: No matches found for first query match sequence: `Descendants matching type NavigationBar` -> `Descendants matching type Button`, given input App element pid: 89909 (no attribute values faulted in)
Possibly caused by runtime issues:

如果有人遇到过这个问题,我将不胜感激。

最好的问候, 鸡

编辑:经过更多的实验,我有了更多的信息。 正常运行应用程序创建settingsBarButton时,isAccessibilityElement返回true,但在测试过程中返回false。

【问题讨论】:

    标签: ios swift testing xctest xcode-ui-testing


    【解决方案1】:

    看起来按钮不是导航栏的子视图。

    改用app.buttons["settingsBarButton"].tap()


    这个小扩展有时会有所帮助。

    extension XCUIElement {
        func tapUnhittable() {
            XCTContext.runActivity(named: "Tap \(self) by coordinate") { _ in
                coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0)).tap()
            }
        }
    }
    

    试试app.buttons["settingsBarButton"].tapUnhittable()

    【讨论】:

    • 它是 debugDescription 导航视图下的缩进,我可以打印和测试 exists,只是 tap() 不起作用。
    【解决方案2】:

    也许测试按钮是否实际工作或测试是否正确失败。如果按钮正在工作并且测试仍然失败,那么您可能定位错误的元素。您可以尝试的一件事是尝试直接点击元素:

    app.buttons["settingsBarButton"].tap()
    

    如果这也不起作用,请尝试:

    if app.otherElements["settingsBarButton"].exists {
        app.otherElements["settingsBarButton"].tap()
    }
    

    此外,由于页面正在加载,该元素可能尚未出现。这种情况下,可以尝试等待元素加载完毕,

    let settingButton = app.buttons["settingsBarButton"]
    let predicate = NSPredicate(format: "exists == true")
    let theExpectation = expectation(for: predicate, evaluatedWith: settingButton, handler: nil)
    _ = XCTWaiter().wait(for: [theExpectation], timeout: 5)
    if settingButton.exists {
        settingButton.tap()
    }
    

    如果测试仍然失败,可能不是测试问题,按钮点击实际上不起作用。因此,请务必检查您的代码。

    【讨论】:

    • 以上我都做了。我测试了existshittable 的按钮,它们返回true,但tap() 找不到按钮。
    • 等待期待呢?此外,有时根据您在哪个模拟器设备上运行测试,您可能需要向上或向下滑动页面。这可能值得一试。
    • 尝试this 回答,他们似乎面临同样的问题。
    • 所以,是的。好像按钮真的不行,我用的是点击坐标位置的方法。
    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    相关资源
    最近更新 更多