iOS XCUIT通过辅助功能访问元素 [英] iOS XCUITests access element by accesibility

查看:108
本文介绍了iOS XCUIT通过辅助功能访问元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过他的accesibilityLabel或标识符声明按钮存在?

How do i assert a button exists by his accesibilityLabel or identifier?

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 accesing the text property i want by specific id, maybe the text property override the accesibiltyLabel?
}

推荐答案

在应用程序代码中设置辅助功能标识符,然后在测试中使用该标识符搜索按钮。 / p>

Set an accessibility identifier in your application code, and then search for the button using that identifier in your tests.

// 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用户以向其解释元素。

The accessibility identifier is set independently of text on the button, and is also independent of the accessibility label. It's not best practice to put identifiers for UI elements as the accessibility label, since the accessibility label is read to VoiceOver users to explain the element to them.

这篇关于iOS XCUIT通过辅助功能访问元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆