Sate Nheehan Sate Nheehan - 2 months ago 19
Swift Question

XCUITest Multiple matches found error

I am writing tests for my app and need to find the button "View 2 more offers" there are multiple of these buttons on my page but I would just like to click on one. When I try this, an error comes saying "Multiple matches found"
So the question is, what ways can I go around this so my test will search and tap on only one of the buttons called "View 2 more offers".

Here is my current code

let accordianButton = self.app.buttons["View 2 more offers"]
if accordianButton.exists {
accordianButton.tap()
}
sleep(1)
}

Answer

You should use a more elaborated way to query your button, since there is more than one button who's matching it.

    // We fetch all buttons matching "View 2 more offers" (accordianButtonsQuery is a XCUIElementQuery)
    let accordianButtonsQuery = self.app.buttons.matchingIdentifier("View 2 more offers")
    // If there is at least one
    if accordianButtonsQuery.count > 0 {
        // We take the first one and tap it
        let firstButton = accordianButtonsQuery.elementBoundByIndex(0)
        firstButton.tap()
    }
Comments