XCUITest无法检测动态生成的tableView的可访问性ID

use*_*712 1 iphone ios swift xcode-ui-testing

我正在使用XCUIApplication编写UI测试.我正在测试单元格行上的选定索引.当我运行测试时,我无法获得所选索引的文本.但是,我可以单击所选行,但使用底部tableView的静态文本.当下面有tableViewController时会发生这种情况.我试图在另一个TableViewController的顶部添加一个带有tableView的ViewController.下面的tableViewController是使用storyboard生成的,但上面的viewController是动态生成的.

设置:我正在使用xib作为tableView的行,我正在使用带有标签和图像的自定义UITableViewCell.标签和图像都打开了辅助功能,但没有打开细胞.我试过在单元格上打开辅助功能但仍然无法正常工作.

这是我的考验

//the BoxListTable is my ViewController that is on top
// I added the following code in that viewController:
   self.view.isAccessibilityElement = true
   self.view.accessibilityIdentifier = "BoxListTable"

//I also added these for the tableView but it doesn't show up in my tests
   tableView.accessibilityIdentifier = "tableview"
   tableView.isAccessibilityElement = true

//my test is the following

 XCUIApplication().tables["BoxListTable"].tap()
Run Code Online (Sandbox Code Playgroud)

当我运行此测试时,测试单击完整的tableView但无法获取任何静态文本.它也无法获取任何单元格或tableview.po XCUIApplication.tables ["BoxListTable"].cells.count返回0. //我试过
XCUIApplication.tables ["BoxListTable"].element.children(匹配:.other)
返回0

你能否告诉我为什么即使设置了accessibilityIdentifier也无法看到它.

小智 6

首先,如果你打开isAccessibilityElement父视图上的标志(在你的情况下是它的视图BoxListTable),它的所有子节点都将无法访问.这同样适用于UITableViewUITableViewCell.但是您可以设置它们,accessibilityIdentifiers并且它们对于所有UI元素都是可见的,甚至是那些isAccessibilityElement评估为的元素false.

所以有这个纯XCTest版本,但它只适用于已加载的单元格(所有延迟加载的单元格将无法使用),并可能被键盘或导航栏阻碍.此外,如果您查找元素的内容而不对其执行任何操作,框架本身将不会滚动到此元素.因此,检查视图exists是否会通过但isHittable会失败,因为元素可能不在用户可以看到的区域之外.

let app = XCIUApplication()
let selectedCell: XCUIElement = table.cells.element(matching: NSPredicate(format: "isSelected == true"))
selectedCell.tap() // If you want to make sure framework will scroll to it.
selectedCell.staticTexts["yourText"]
// or
selectedCell.staticTexts.element(boundBy: index)
Run Code Online (Sandbox Code Playgroud)

我鼓励您尝试使用AutoMate库,它可以为您节省上述所有麻烦.

let app = XCIUApplication()
let selectedCell: XCUIElement = table.cells.element(matching: NSPredicate(format: "isSelected == true"))
XCTAssertFalse(selectedCell.isVisible) // `isVisible` checks if element exists in hierarchy and is visible to the user.
table.swipe(to: .down, untilVisible: selectedCell, from: app)
XCTAssertTrue(selectedCell.isVisible)
Run Code Online (Sandbox Code Playgroud)

要查看所有这些方法是如何工作的,请查看我在此处制作的示例应用程序<<.

如果你发现任何isSelected财产问题,XCUIElement你应该检查我的回答>>这个问题<<.正如您在示例应用程序中看到的,我遇到了这个问题(Xcode版本8.3.2(8E2002)).

  • 顺便说一句,设置 `isAccessibilityElement = false` 确实让我在相关的 UI 测试问题上度过了愉快的时光......非常感谢您的提示:) (2认同)