iOS XCUIT通过辅助功能访问元素

God*_*her 14 ios xctest swift

如何通过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?
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ole*_*tha 23

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

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

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

可访问性标识符的设置与按钮上的文本无关,也与可访问性标签无关.将UI元素的标识符作为可访问性标签放入最佳实践不是最佳做法,因为可访问性标签被读取给VoiceOver用户以向其解释元素.


Sco*_*des 11

重要说明:如果将超级视图设置为可访问,则XCUITest可能无法访问其子视图.

您可以通过故事板或以编程方式设置其可访问性来访问该元素,如上所述.当光标位于以前缀"test"开头的函数中时,可以单击记录按钮,以记录XCUITest如何看到元素.有时它需要一些清理(命令转换k)和几分钟的记录按钮可用.您还可以从故事板中逐步下载树并使用XCUITest函数(如element(boundBy:Int),子元素(匹配:.textField)),也可以将它们链接起来:XCUIApplication().tables.cells.containing(.button,标识符:"id").在那之后是简单的部分,使用返回布尔值的.exists.

在此输入图像描述

  • 第一句提供了非常有用的信息,并结束了长达一小时的错误搜索. (4认同)

Ste*_*fan 7

添加| "accessibilityIdentifier"字符串测试| 在导航栏上的"用户定义的运行时属性"中,而不是在辅助功能标签中