gpt4 book ai didi

iOS XCUI通过可访问性测试访问元素

转载 作者:IT王子 更新时间:2023-10-29 05:28:24 25 4
gpt4 key购买 nike

我如何通过 accessibilityLabel 或标识符断言按钮存在?

func testExitsButton() {
XCTAssertTrue(app.windows.containing(.button, identifier: "Button Text").element.exists)
XCTAssertTrue(app.buttons["Button Text"].exists)
XCTAssertTrue(app.buttons["test"].exists) <- I want this, instead of accessing the text property I want by specific id, maybe the text property override the accessibilityLabel?
}

enter image description here

最佳答案

在您的应用程序代码中设置可访问性标识符,然后在您的测试中使用该标识符搜索按钮。

// app code
let button: UIButton!
button.accessibilityIdentifier = "myButton"

// UI test code
func testMyButtonIsDisplayed() {
let app = XCUIApplication()
let button = app.buttons["myButton"]
XCTAssertTrue(button.exists)
}

无障碍标识符的设置独立于按钮上的文本,也独立于无障碍标签。将 UI 元素的标识符作为可访问性标签并不是最佳做法,因为可访问性标签是向 VoiceOver 用户朗读以向他们解释该元素。

关于iOS XCUI通过可访问性测试访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41442932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com