ios - EarlGrey - 如何找到任何带有特定文本的表格视图单元格
问题描述
我有一个带有标签的自定义 UITableViewCell 。单元格和标签都有一个可访问性标识符,我试图断言在 UI 中至少有一个包含指定文本的单元格。我这样写断言:
EarlGrey
.selectElement(with: grey_text(self.customTestString))
.using(searchAction: grey_scrollInDirection(GREYDirection.down, 45), onElementWithMatcher: grey_kindOfClass(UILabel.self))
.assert(grey_sufficientlyVisible())
属性 customTestString 包含我要查找的字符串。我还尝试寻找我的 UITableViewCell 自定义子类,但无论如何我都遇到了这个失败:
Exception: MultipleElementsFoundException
Exception Name: MultipleElementsFoundException
Exception Reason: Multiple UI elements matched for given criteria.
Exception with Assertion: {
"Assertion Criteria": "assertWithMatcher:matcherForSufficientlyVisible(>=0.750000)",
"Element Matcher": "((kindOfClass('UILabel') || kindOfClass('UITextField') || kindOfClass('UITextView')) && hasText('TEST_STRING'))",
"Recovery Suggestion": "Create a more specific matcher to narrow matched element"
}
Exception Details: Search action: <GREYScrollAction: 0x600001a44b40>.
Search action element matcher: kindOfClass('iComplain.ComplaintTableViewCell').
Error Trace: [
{
"Description": "Multiple elements were matched: (
"<UILabel:0x7fa653455880; AX=Y; AX.label='TEST_STRING'; AX.frame={{15, 209}, {85, 16}}; AX.activationPoint={57.5, 217}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{15, 32}, {85, 16}}; alpha=1; UIE=N; text='TEST_STRING'>",
"<UILabel:0x7fa653455600; AX=Y; AX.label='TEST_STRING'; AX.frame={{15, 182}, {107.5, 20.5}}; AX.activationPoint={68.75, 192.25}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{15, 5}, {107.5, 20.5}}; alpha=1; UIE=N; text='TEST_STRING'>"
). Please use selection matchers to narrow the selection down to single element.",
"Error Domain": "com.google.earlgrey.ElementInteractionErrorDomain",
"Error Code": "5",
"File Name": "GREYElementInteraction.m",
"Function Name": "-[GREYElementInteraction grey_errorForMultipleMatchingElements:withMatchedElementsIndexOutOfBounds:]",
"Line": "965",
"TestCase Class": "iComplainTests.EarlGreyComplaintUITests",
"TestCase Method": "testWriteComplaint"
}
]
据我了解,它抱怨断言不明确,因为它可以在多个单元格上运行,但我不知道如何解决它。有什么提示吗?
解决方案
gray_scrollInDirection 期望视图层次结构中可以滚动的元素的方向和匹配器。尝试在 gray_scrollInDirection 方法中传递表格视图的可访问性标识符。有关更多信息 - 您可以参考此https://github.com/google/EarlGrey/blob/master/docs/api.md#selecting-off-screen-ui-elements。
我建议使用 UI 元素的可访问性标识符作为 selectElement 方法的匹配器,因为这是强烈推荐的,因为您提到其中的单元格和标签都具有可访问性标识符。要缩小到您在视图层次结构中的 UI 元素列表中查找的元素,您可以形成匹配器以使用 gray_allOf 匹配器选择元素(标签)并将其传递给这样的匹配器数组 -
EarlGrey
.selectElement(with: grey_allOf([grey_accessibilityID(labelID), grey_ancestor(grey_accessibilityID(cellID)), grey_sufficientlyVisible()]))
.usingSearch(action: grey_scrollInDirection(.down, 45), onElementWith: grey_accessibilityID(tableViewID))
.assert(grey_notNil())
上面的代码查找一个元素 - 它具有标识符 labelID,具有标识符为 cellID(您的 tableViewCell)的祖先,并且通过在具有标识符 tableID 的表视图上滚动,它是视图层次结构中足够可见的元素。usingSearch 方法在标识符为 tableID 的 tableView 上重复向下滚动 45 个点,直到找到元素并且断言成功,或者到达滚动视图的末尾并且断言失败。您可以在此处参考选择 API 以获取更多信息 - https://github.com/google/EarlGrey/blob/master/docs/api.md#selection-api。
推荐阅读
- c++ - 在 std::vector 中存储指针
- html - 使用 CSS 将图像块居中
- java - Maven 无法识别安装在 Ubuntu shell 上的 $JAVA_HOME jdk
- html - 如何在悬停时也有框阴影效果的表格行之间放置空间?
- c++ - 如何等待多个线程完成并重用它们?
- python - RPI 在启动时打开终端并自动运行循环 python 脚本
- c++ - c ++程序计算字母字符的出现次数
- tkinter - 尝试使用 Tka11y
- java - string.split(\\s+) 无法处理前导空格
- python - 卸载用户在 macOS High Sierra 上使用 pip 安装的所有软件包