Aleh Sarochych
Aleh Sarochych

Reputation: 183

Xcode 7, UI-Testing: working with UITableView

I'm facing with one problem using UITesting framework for the xCode that Apple has introduced at WWDC 2015. I have a UITableView and this table contains a lot of cells. Also I have a NSArray with cells titles - if cell title is contained in NSArray this cell should be tapped. My problem is that I can't scroll table view for particular cell, because framework doesn't contain method for working with table views, only swipe gestures (down, up).

Maybe somebody knows how I can tap on particular cell in table view? Or how I can scroll table view for particular cell? Because when I call tap() method for cell which are not visible at screen - nothing happen.

Thanks in advance!

Upvotes: 18

Views: 12094

Answers (4)

Santosh
Santosh

Reputation: 2914

Swift version for @Eugene Zhenya Gordin's answer: Updated for Swift 4

    let tablesQuery = app.tables
    let cellQuery = tablesQuery.cells.containing(.staticText, identifier: "MyCell")
    let cell = cellQuery.children(matching: .staticText)
    let cellElement = cell.element
    cellElement.tap()

    let tableElement = tablesQuery.element
    tableElement.swipeUp()

Upvotes: 5

ArielSD
ArielSD

Reputation: 909

I was having the same exact issue!

At first, I was just tapping on the cell's label, through the .staticTexts query. Here's another way that worked for me:

In cellForRowAtIndexPath, where you instantiate the cells, give them each a unique identifier, depending on their titleLabel or another property.

Example:

cell.accessibilityIdentifier = [NSString stringWithFormat:@"myCellType%@", cell.colorIdentifier];

Then, back in the test class, try this: (A little verbose on purpose, because it helped me understand better)

XCUIApplication *application = [XCUIApplication new];
XCUIElementQuery *tablesQuery = application.tables;
XCUIElementQuery *allCellsQuery = tablesQuery.cells;
XCUIElementQuery *specificCellQuery = [cellQuery matchingIdentifier:@"myCellTypeSaffron"];
XCUIElement *cell = specificCellQuery.element;

[cell tap];

The specific cell resolves when it's interacted with.

Hope that helps!

Upvotes: 5

Eugene Gordin
Eugene Gordin

Reputation: 4107

This worked for me:

XCUIElementQuery *tablesQuery = self.app.tables;

XCUIElementQuery *cellQuery = [tablesQuery.cells containingType:XCUIElementTypeStaticText
                                                     identifier:@"Work"];

XCUIElementQuery* cell = [cellQuery childrenMatchingType:XCUIElementTypeStaticText];

XCUIElement* cellElement = cell.element;

[cellElement tap];

For scrolling the table use:

XCUIElementQuery *tablesQuery = self.app.tables;
XCUIElement* table = tablesQuery.element;
[table swipeUp];

Upvotes: 17

Anant Goel
Anant Goel

Reputation: 51

I recently came across a similar problem. An option you can try is to look for a specific title for a cell in the table, and tap on that,

for example:-

XCUIApplication().tables.staticTexts["identifier"].tap()

or

XCUIApplication().tables.cells.staticTexts["identifier"].tap()

Where "identifier" is the title of the cell you are trying to tap.

I hope this helps!

Upvotes: 5

Related Questions