Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select first cell in collection view from UITest

Is there a way to select or trigger didSelect from a UITest on the first cell if it exists in a collection view?

When recording it uses static text from the selected cell. If the cell is populated from the network with dynamic content and with the collection view to possibly contain no cells, this test will break.

like image 356
some_id Avatar asked Dec 29 '15 17:12

some_id


1 Answers

You can select the first cell in a collection view with:

let app = XCUIApplication()
app.launch()

let firstChild = app.collectionViews.childrenMatchingType(.Any).elementBoundByIndex(0)
if firstChild.exists {
    firstChild.tap()
}

Swift 3

let firstChild = app.collectionViews.children(matching:.any).element(boundBy: 0)
if firstChild.exists {
     firstChild.tap()
}

On a more theoretical note, your test suite should use deterministic data. You should always know exactly how many cells, and what they contain, will be returned from the service. You can accomplish this by using a seeded development server or mocking out network requests when running your test suite.

like image 159
Joe Masilotti Avatar answered Oct 15 '22 18:10

Joe Masilotti