XCTestCase:无法在导航栏中按下自定义栏按钮

问题描述 投票:0回答:2

我正在为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:

如果有人遇到此问题,我将非常感谢。

最好的问候,鸡

ios swift testing xctest xcode-ui-testing
2个回答
0
投票

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

改为尝试app.buttons["settingsBarButton"].tap()


0
投票

一种选择是在尝试点击之前检查元素是否存在,],>

if app.navigationBars.buttons.firstMatch.exists {
    app.navigationBars.buttons.firstMatch.tap()
}

您可以尝试的另一件事是尝试直接点击元素:

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()
}
    
© www.soinside.com 2019 - 2024. All rights reserved.