【问题标题】:Uitest refresh control swiftUitest 刷新控制 swift
【发布时间】:2018-07-10 09:51:14
【问题描述】:

我正在尝试为刷新控件添加 uitest,但我无法做到,因为我无法使用可访问性标识符 (refreshControl.accessibilityIdentifier = "refreshControlList") 访问刷新控件

launchAppWithArgs(json: OrdersResultJSON.ok, xcuiApp: app)

        let table = app.tables["agendaTable"]

        DispatchQueue.main.async {
            table.otherElements["sectionHeader0"].press(forDuration: 2, thenDragTo: table.otherElements["sectionHeader2"])
        }

        if !table.otherElements[""].waitForExistence(timeout: 6) {
            print("XXX")
        }

有什么测试的建议吗?

【问题讨论】:

    标签: swift testing integration-testing xcode-ui-testing ui-testing


    【解决方案1】:

    要执行拉动刷新,只需获取第一个单元格并将其向下拖动:

    let firstCell = app.tables["agendaTable"].cells.firstMatch
    let start = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0))
    let finish = firstCell.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 10))
    start.press(forDuration: 0, thenDragTo: finish)
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      您可以获取 UIRefreshControl 的标题标签并在其上放置一个标识符,如下所示:

      func setSubviewAccessibility(for tableView: UITableView) {
                  guard let titleLabel = tableView.refreshControl?.subviews.first?.subviews.last as? UILabel else {
                      return
                  }
                  titleLabel.isAccessibilityElement = true
                  titleLabel.accessibilityIdentifier = "refresh_control_label"
              }
      

      调用此方法传递您已为其设置 UIRefreshControl 的表视图,您应该一切顺利。

      那么,在测试方面:

      let refreshControl = XCUIApplication().staticTexts["refresh_control_label"]
      
                  guard refreshControl.waitForExistence(timeout: 5) else {
                      return XCTFail("Refresh control label not found.")
                  }
      

      剩下的唯一问题是您可能需要将列表加载时间稍长于一秒,这样您的测试才不会错过 UIRefreshControl。使用 waitForExistence 总是好的。

      【讨论】:

      • 对我来说这个问题是它不会进入下一行,直到 refreshControl 再次被隐藏,这使得它无法通过测试
      • 你使用了waitForExistence吗?
      • 是的,我用过我不认为这是问题所在,因为到那时,刷新控件被隐藏了。我什至试图把它放在我的拉动操作之前,并在调用下拉之前等待 5 秒
      猜你喜欢
      • 2016-07-28
      • 1970-01-01
      • 2023-04-01
      • 2010-11-22
      • 1970-01-01
      • 2013-03-31
      • 2015-03-11
      • 2010-10-23
      • 1970-01-01
      相关资源
      最近更新 更多