如何在异步加载时使用XCUITest验证UITableView中的单元格数量?

时间:2017-10-12 15:34:31

标签: ios swift uitableview xcuitest

我正在编写一个需要异步加载数据的UI测试,然后对单元格中显示的数据进行一些验证。

在这种情况下,如果没有数据加载测试通过,则不应该这样,我需要确保我至少有一个基于远程数据加载的单元格。

func testTableViewCellDoesNotContainItem() {
  ...setup....

  // Load data 
  app.keyboards.buttons["Search"].tap()
  XCTAssert(app.tables["TableView"].cells.count > 0)

  ...other check...
}

1 个答案:

答案 0 :(得分:1)

能够通过以下方式实现预期效果:

func testTableViewCellDoesNotContainItem() {
  ...setup....

  // Prep expectation for async load ensuring that we have at least one cell and the service call worked
  let loaded = NSPredicate(format: "count > 0")
  expectation(for: loaded, evaluatedWith: app.tables["TableView"].cells, handler: nil)

  // Load data 
  app.keyboards.buttons["Search"].tap()
  waitForExpectations(timeout: 5, handler: nil)

  ...other check...
}
相关问题