【问题标题】:Finding an element in UI Test with only substring Xcode在 UI 测试中仅使用子字符串 Xcode 查找元素
【发布时间】:2018-03-09 14:49:57
【问题描述】:

我的应用中有一个用于创建帐户的 Web 视图(我没有编写,也无法更改关联的网页)。

通常,单击任何按钮/链接都很容易,但这里我的“创建帐户”按钮的字符串在其标题中有一个很棒的字体,这会使记录器崩溃并且无法以编程方式调用该按钮。

这是它在app.webViews.allElementsBoundByIndex 中打印的内容:

Element subtree:
 →WebView, 0x1c03823c0, traits: 8589934592, {{0.0, 0.0}, {736.0, 414.0}}
...
Link, 0x1c0384100, traits: 8590065666, {{242.0, 357.0}, {252.0, 44.0}}, label: 'create account '

如您所见,字体 awesome 变成了 ,我无法通过以下方式检测到:

webViews.staticTexts["create account "].tap()

问题

  1. 如何仅使用查询的子字符串查看 staticTexts(例如 webViews.staticTexts["create acco"].tap())?
  2. 有没有更好的方法来查找并点击此按钮和相关链接?

【问题讨论】:

    标签: xcode xcode-ui-testing ui-testing uiaccessibility


    【解决方案1】:

    您可以使用NSPredicate 查找包含部分单词/短语的静态文本,使用XCUIElementQuery 上的containing(_:) 方法。

    let predicate = NSPredicate(format: "label CONTAINS 'create account'")
    let app = XCUIApplication()
    let createAccountText = app.webViews.links.containing(predicate)
    createAccountText.tap()
    

    【讨论】:

      【解决方案2】:

      在下标上使用starts(with: Sequence) 没有捷径。因此,您应该遍历每个文本字段并检查其标签是否以 create account 开头:

      func testStartsWith() {
          let app = XCUIApplication()
          let staticTexts = app.webViews.staticTexts // or app.webViews.links
          for i in 0..<staticTexts.count {
              let text = staticTexts.element(boundBy: i)
              if text.label.starts(with: "create account") {
                  text.tap()
                  break
              }
          }
      }
      

      注意: 因为您使用的是 WebView,所以 staticTexts 元素比 links 元素多得多。根据您提供的打印输出,您的create account  视图具有链接特征,因此您应该能够使用app.webViews.links 进行更快的匹配。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-01
        相关资源
        最近更新 更多