gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:08 25 4
gpt4 key购买 nike

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

设置:我正在为 tableView 的行使用 xib,我正在使用带有标签和图像的自定义 UITableViewCell。 Label 和 image 都打开了辅助功能,但单元格没有。我已经尝试在单元格上打开辅助功能,但仍然没有用。

这是我的测试

//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()

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

能否请您告诉我为什么我看不到 accessibilityIdentifier,即使它已设置。

最佳答案

首先,如果您在父 View (在您的情况下是 BoxListTable 的 View )上打开 isAccessibilityElement 标志,则其所有 subview 都将不可访问.这同样适用于 UITableViewUITableViewCell。但是您可以设置它们的 accessibilityIdentifiers,它们将对所有 UI 元素可见,即使那些 isAccessibilityElement 评估为 false

所以有这个纯 XCTest 版本,但它只适用于已加载的单元格(所有延迟加载的单元格将不可用)并且可能会被键盘或导航栏阻挡。此外,如果您查找元素的内容而不对其执行任何操作,框架本身将不会滚动到该元素。因此,检查 View 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)

我鼓励您尝试使用 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)

要了解所有这些方法的工作原理,请查看我制作的示例应用程序 >>here<< .

如果您发现 XCUIElement 上的 isSelected 属性有任何问题,您应该查看我对 >>this question<< 的回答.正如您在示例应用程序中看到的那样,我遇到了这个问题(Xcode 版本 8.3.2 (8E2002))。

关于ios - XCUITest 无法检测到我动态生成的 tableView 的可访问性 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43812218/

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