4

I am very new to UI test. I have an UITableView in my storyboard and it contains some cells. Update: I want to assert that the num of cells in UITableView when the app launches will be more than 0. But I don't know how to code this part.Using NSPredicate? Or others?

 func testCellsNum()
{
        let app = XCUIApplication()
        let tableCell = app.tableRows.count
    //Then what should I do ?
        XCTAssertGreaterThan(tableCell, 0, "should greater than 0")//this line doesn't work


}
beasone
  • 1,073
  • 1
  • 14
  • 32

1 Answers1

17

If you only have one table:

func testExample() {
    let app = XCUIApplication()
    let tablesQuery = app.tables
    let count = tablesQuery.cells.count
    XCTAssert(count > 0)
}

If you have multi tables,using this to get first or any index you want

 tablesQuery.elementAtIndex(0).cells
Leo
  • 24,596
  • 11
  • 71
  • 92
  • I have two scenes. The first one has UITableView. When I click the first cell in it, it will enter the second scene. There is ImageCells on the second scene. How could I test the num of images cells in the second scene? – beasone Nov 11 '15 at 03:34
  • See the the post I answered here http://stackoverflow.com/a/33643956/3940672,you can use recoding to generate original code,then make a little changes with the code I post – Leo Nov 11 '15 at 03:51
  • You could also add an **accessibilityIdentifier** to your tableview like `myTableView.accessibilityIdentifier = “myUniqueTableViewIdentifier”`, once done, you can access the tableView in your test case like: `let myTableView = app.tables.matching(identifier: "myUniqueTableViewIdentifier")` – Kushal Ashok Sep 04 '17 at 08:59
  • Can anyone confirm this works in iOS 11, Xcode 9? The assert always fails for me, even with static cells. – damjandd Dec 28 '17 at 09:37
  • @damjandd I'm running into the same issue, might be related to: https://stackoverflow.com/questions/33679817/wrong-cells-count-for-collection-view-in-ui-tests – JoniVR Jun 16 '18 at 17:11