【问题标题】:Xcode UITest scrolling to the bottom of an UITableViewXcode UITest 滚动到 UITableView 的底部
【发布时间】:2019-06-16 09:07:08
【问题描述】:

我正在编写一个 UI 测试用例,我需要在其中执行一个操作,然后在当前页面上,将唯一的 UITableView 滚动到底部以检查特定文本是否显示在 @ 的最后一个单元格内987654322@。

现在我能想到的唯一方法是使用app.tables.cells.element(boundBy: 0).swipeUp() 滚动它,但如果单元格太多,它不会一直滚动到底部。 UITableView 中的单元格数量并不总是相同,我不能多次向上滑动,因为表格中可能只有一个单元格。

【问题讨论】:

    标签: xcode uitableview xctest xcuitest uitest


    【解决方案1】:

    解决此问题的一种方法是从 tableView 中获取最后一个单元格。然后,运行一个滚动的while循环并检查每个滚动之间的单元格isHittable。一旦确定isHittable == true,就可以断言该元素。 https://developer.apple.com/documentation/xctest/xcuielement/1500561-ishittable

    看起来像这样(Swift 回答):

    1. 在您的XCTestCase 文件中,编写一个查询来标识该表。然后,进行后续查询以识别最后一个单元格。
    let tableView = app.descendants(matching: .table).firstMatch
    guard let lastCell = tableView.cells.allElementsBoundByIndex.last else { return }
    
    1. 使用 while 循环来确定单元格 isHittable/ 是否在屏幕上。 注意: isHittable 依赖于单元格的 userInteractionEnabled 属性设置为 true
    //Add in a count, so that the loop can escape if it's scrolled too many times
    let MAX_SCROLLS = 10
    var count = 0
    while lastCell.isHittable == false && count < MAX_SCROLLS {
        apps.swipeUp()
        count += 1
    }
    
    1. 使用label 属性检查单元格的文本,并将其与预期的文本进行比较。
    //If there is only one label within the cell
    let textInLastCell = lastCell.descendants(matching: .staticText).firstMatch
    XCTAssertTrue(textInLastCell.label == "Expected Text" && textInLastCell.isHittable)
    

    【讨论】:

      【解决方案2】:

      Blaines 的回答让我更深入地研究了这个主题,我找到了一个对我有用的不同解决方案:

      func testTheTest() {
          let app = XCUIApplication()
          app.launch()
      
          // Opens a menu in my app which contains the table view
          app.buttons["openMenu"].tap()
      
          // Get a handle for the tableView
          let listpagetableviewTable = app.tables["myTableView"]
      
          // Get a handle for the not yet existing cell by its content text
          let cell = listpagetableviewTable.staticTexts["This text is from the cell"]
      
          // Swipe down until it is visible
          while !cell.exists {
              app.swipeUp()
          }
      
          // Interact with it when visible
          cell.tap()
      }
      

      为了工作,我必须做的一件事是将isAccessibilityElement设置为true,并将accessibilityLabel作为字符串分配给表格视图,以便它可以在测试代码中对其进行查询。

      这可能不是最佳实践,但就我在测试中看到的情况而言,它运行良好。我不知道当单元格没有文本时它会如何工作,人们可能能够通过图像视图或其他东西来引用单元格(这里没有真正直接引用)。显然,Blaines 的回答中缺少计数器,但出于简单原因,我将其省略了。

      【讨论】:

      • 太棒了!如果我能在 2 小时前发现这个问题,就可以避免这么多的挫折
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 2016-07-22
      • 2013-09-12
      • 2012-01-07
      • 1970-01-01
      • 2013-05-05
      相关资源
      最近更新 更多