【问题标题】:How can we query for an UI element with more than one attribute in XCUITest我们如何在 XCUITest 中查询具有多个属性的 UI 元素
【发布时间】:2021-05-19 07:17:11
【问题描述】:

我在为我的 UI 测试定位 iOS 应用上的 UI 元素时遇到问题。一般策略是使用单个属性定位元素。类似的东西。
XCUIApplication().staticTexts["Some text on the UI"]
如果存在,这会在 UI 上为您提供一个匹配的元素。有没有一种方法可以使用多个属性来查询 UI?类似
XCUIApplication().staticTexts[type ="Heading", value = "Some text on the UI"].
我希望它返回一个元素,而不是像 .matching() 这样的查询。

【问题讨论】:

  • 您从哪里获得type 属性? staticText 通常只有 labelidentifier,它们通常匹配。有一些东西叫做accessibilityTraits,但使用起来会有些困难。如果你能提供一个更好的例子来说明你正在使用什么,我相信我们可以通过将多个 matching() 串在一起或使用谓词来找到答案。

标签: ios swift xcuitest


【解决方案1】:

你可以写一个谓词。但是,您可以依赖的字段相当有限 - 我不知道这个 type=Heading 是什么,但我不相信您能够查询它。您可以查询不同的 XCUIElementTypes,但您已经在此处将候选对象缩减为仅 staticTexts,因此在这种特殊情况下无济于事。

类似:

XCUIApplication().staticTexts.matching(NSPredicate(format: "value = 'Some text on the UI' or identifier = 'myIdentifier'")).firstMatch

您可以依赖的字段是 XCUIElementAttributes:https://developer.apple.com/documentation/xctest/xcuielementattributes

谓词编程指南:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Predicates/AdditionalChapters/Introduction.html

【讨论】:

  • 谢谢,大卫,这有帮助。我很高兴我走的是正确的道路。真可惜 XCUITest 没有太多要查询的属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多