XCUIAutomation: Click on corresponding button?

167 views Asked by At

enter image description hereHow to click on a button called Unlock from the label text LE SR Home notour. I'm new to this tool and got stuck here. enter image description here

1

There are 1 answers

6
Mike Collins On

Oddly, your screenshot shows two Unlock buttons, but your debug only shows one.

Going from your debug output, assuming there is only a single unlock button in the application, you simply need to provide the label.

app.buttons["Unlock"].tap()

If there are multiple buttons matched, you need to associate it with the label and find their shared parent. This only works if the parent container isn't shared with the other Unlock button.

app.otherElements.containing(.staticText, identifier: "LE SR Home notour:").lastMatch.buttons["Unlock"].tap()

OR assume its index. If it's the first one on the page firstMatch is what you're looking for.

app.buttons["Unlock"].firstMatch.tap()

If it's not the first you can return an array of all elements (allElementsBoundByIndex) and then choose the index of the one you want. To find the second (arrays begin at 0), for example:

app.buttons["Unlock"].element(boundBy: 1).tap()

or

app.buttons["Unlock"].allElementsBoundByIndex[1].tap() 

The best solution is for you or your development team add an accessibility identifer to this button so it can be uniquely identified.