How to get index of XCUIElement in XCUIElementQuery?

Bartłomiej Semańczyk picture Bartłomiej Semańczyk · Aug 23, 2015 · Viewed 11.3k times · Source

This is my simple UITest (customizing order of tabs in tabbarcontroller):

func testIsOrderOfTabsSaved() {

    let app = XCUIApplication()
    let tabBarsQuery = app.tabBars
    tabBarsQuery.buttons["More"].tap()
    app.navigationBars["More"].buttons["Edit"].tap()
    tabBarsQuery.buttons["Takeaway"].swipeLeft()
    tabBarsQuery.buttons["In Restaurant"].swipeLeft()

    //here, how to get position of moved button with text "In Restaurant"?

NOTE:

It is possible to get XCUIElement from XCUIElementQuery by index. Can I do this fro the other way?

Answer

Alex picture Alex · Aug 27, 2015

It seems that the queries automatically return in order based on position on screen.

for i in 0...tabsQuery.buttons.count {
    let ele = tabsQuery.buttons.elementBoundByIndex(i)
}

Where the index i represents the position of the button in the tab bar, 0 being the leftmost tab, i == tabsQuery.buttons.count being the rightmost.