Arjun Kalidas
Arjun Kalidas

Reputation: 1027

Is there a way to get the number of cells in a table view?

In case if we are to take in all the cells in a TableView into an array and iterate through it to click on elements. I am looking for a solution in swift.

Upvotes: 0

Views: 738

Answers (2)

sgl0v
sgl0v

Reputation: 1417

This could be achieved by implementing a custom GREYAssertionBlock for the UITableView:

func assertTableView(_ accessibilityID: String, hasRowCount rowCount: Int, inSection section: Int) {
    let cellCountAssert = GREYAssertionBlock(name: "cell count") { (element, error) -> Bool in
        guard let tableView = element as? UITableView, tableView.numberOfSections > section else {
            return false
        }
        let numberOfCells = tableView.numberOfRows(inSection: section)
        return numberOfCells == rowCount
    }

    EarlGrey.selectElement(with: grey_accessibilityID(accessibilityID)).assert(cellCountAssert)
}

Upvotes: 0

Arjun Kalidas
Arjun Kalidas

Reputation: 1027

If each cell have a differentiating factor, then you get those in an array. To use the text strings, I have to get hold of the cells first. So that leads to - how to get count count of cells and after getting hold of the cells, drilling down to see if this is the text, then open the contextual menu, else do something.

Here's what you can do: Keep using 'atIndex:' on the cells. Use selectElementWithMatcher::withError. Loop through until you find an indexOutOfBoundsError and then you should have the text.

But with the looping using atIndex:, you should have the cells that you want. And to do the same, see below:

for (int i = 0; i < someLargeValue; i++) {
 EarlGrey.selectElementWithMatcher(grey_accessibilityID("abc")).atIndex(i)
}

-> Ok and for getting the value of “someLargeValue”, use selectElementWithMatcher::withError. Loop through until you find an indexOutOfBoundsError and then you should have the text.

Upvotes: 1

Related Questions