Xcode 7, UI-Testing: working with UITableView
Asked Answered
V

4

18

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!

Valais answered 14/9, 2015 at 10:41 Comment(1)
if my answer resolved your issue, can you please accept it, thank youCalendra
C
17

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];
Calendra answered 17/12, 2015 at 20:29 Comment(3)
any idea how to achieve this with Swift?Pivoting
@Pivoting See my answer for Swift equivalent.Persuasion
let app = XCUIApplication() let tablesQuery = app.tables let cellQuery = tablesQuery.cells.containing(.staticText, identifier: "Work") let cell = cellQuery.children(matching: .staticText) let cellElement = cell.element cellElement.tap()Fieldstone
F
5

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!

Fireboard answered 24/9, 2015 at 13:56 Comment(1)
Thanks for the reply, but it doesn't work, nothing happen after tap() :/Valais
P
5

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()
Persuasion answered 13/9, 2016 at 18:3 Comment(2)
these won't compile in Swift 4Want
@delta2flat try nowPersuasion
F
5

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!

Finny answered 9/12, 2016 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.