Search code examples
iosswiftxcodexctestxcode-ui-testing

Is it possible to operate with the last of matching elements in XCTest?


I would like to tap the last [Play] button in my app, and I'm looking for something like

app.buttons["play"].lastMatch.tap()

Is there any way to do so?


Solution

  • I managed this problem by writing a little extension

    extension XCUIElementQuery {
        var lastMatch: XCUIElement { return self.element(boundBy: self.count - 1) }
    }
    

    after that, I can simply write code like this

    app.buttons.matching(identifier: "play").lastMatch.tap()