我有以下层次结构: ViewController -> TableView -> TableViewCell -> CollectionView -> CollectionViewCell -> UIImage
在tableViewCell上,collectionView内部,有4个cell。每个细胞都有一个独特的标签。
当用户点击 collectionViewCell 时,单元格将被选中,并且 UIImageView 也会突出显示。在这种情况下,这意味着那里有不同的图像。我启用了 UIImageView 的可访问性,并将特征设置为按钮和图像以尝试访问这些 XCUIElementTypes
我无法使任何 UITest 工作,因为它总是告诉我 UIImageView 有多个结果,即使图像视图所在的单元格具有唯一标识符。
如何断言单元格已被选择?
使用记录功能我得到:
let whatIsYourFilingStatusCellsQuery = XCUIApplication().tables.cells.containingType(.StaticText, identifier:"Single").childrenMatchingType(.Image).matchingIdentifier("btn_unselected").tap()
@Che 有正确的想法。
在
didSelectItemAtIndexPath:
中,我这样做了:
cell.accessibilityIdentifier = @"isSelected";
然后,在测试文件中:
XCTAssertEqualObjects(randomCellToTap.value, @"isSelected");
我也会做相反的瘦
didDeselect...
将选择状态添加到
accessibilityTraits
,如下所示:
cell.accessibilityTraits = [.button, .selected]
然后在 UITest 中,您可以检查
cell.isSelected
状态,这也将降低编程 voice over 状态的复杂性。
其他解决方案将要求您手动实现画外音状态。
如果是 SwiftUI:
VStack {
ZStack {
Circle()
.fill(Color.blue)
.frame(width: 30, height: 30)
if index == selectedIndex {
Image(decorative: "ic_check")
}
}
Text("ブルー")
}
.onTapGesture {
selectedIndex = index
}
.accessibilityAddTraits(index == selectedIndex ? [.isSelected, .isButton] : .isButton)
您甚至可以合并子项。
HStack {
Image("ic_comment", label: Text("コメント数"))
Text("10")
}
.accessibilityElement(children: .combine)
.accessibilityIdentifier("your id")
.accessibilityAddTraits(index == selectedIndex ? [.isSelected, .isButton] : [.isButton] )
我从这里找到它