Xcode UITest滚动到UITableView的底部

标签 xcode uitableview xctest xcuitest uitest

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

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

最佳答案

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

它看起来像这样(快速答案):

  • XCTestCase文件中,编写查询以标识表。然后,进行后续查询以标识最后一个单元格。

  • let tableView = app.descendants(matching: .table).firstMatch
    guard let lastCell = tableView.cells.allElementsBoundByIndex.last else { return }
    
  • 使用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
    }
    
  • 使用label属性检查单元格的文本,并将其与期望的文本进行比较。

  • //If there is only one label within the cell
    let textInLastCell = lastCell.descendants(matching: .staticText).firstMatch
    XCTAssertTrue(textInLastCell.label == "Expected Text" && textInLastCell.isHittable)
    

    关于Xcode UITest滚动到UITableView的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316004/

    相关文章:

    ios - 带有 fetchedResultsController 的 Tableview 在应用重新启动或 View 重新加载之前不会更新新内容

    xcode5 - 是否可以在 Xcode 5 的 Instruments 下运行基于 XCTest 的测试?

    ios - 内部功能的单元代码测试

    ios - 来自太多请求的 NSURLSession 和地理编码器错误,解决方案是什么?

    Xcode 似乎没有执行我的方案的后期操作

    iphone - 用于 iOS 聊天应用程序的 openfire 服务器

    objective-c - 按下按钮时 UITableView 单元格中 UITextField 的清除文本

    ios - InsertSections 函数 Swift

    xcode - xctest - 预处理器宏

    javascript - 如何检测 iPhone 上页面滚动的比例?