iOS XCUITests通过可访问性访问元素 [英] iOS XCUITests access element by accessibility

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

问题描述

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

How do I assert a button exists by his accessibilityLabel 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 accessing the text property I want by specific id, maybe the text property override the accessibilityLabel?
}

推荐答案

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

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元素的标识符作为可访问性 label 放置,因为可访问性标签是由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 XCUITests通过可访问性访问元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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