-1

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

Sudheer
  • 77
  • 1
  • 9
  • Please provide the element tree of your application: https://developer.apple.com/forums/thread/9045. Without seeing the entire UI or how it's coded we cannot provide you a proper answer. – Mike Collins Apr 17 '22 at 16:06
  • Thank you for your reply. I have attached the image with element tree. I have to click on unlock button based on the label text. @MikeCollins can you check and help me. – Sudheer Apr 18 '22 at 20:55

1 Answers1

0

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.

Mike Collins
  • 4,108
  • 1
  • 21
  • 28
  • Hi @MikeCollins, I appreciate your detailed response. The app will have many Unlock buttons. I have to click on the Unlock button based on the label. So I should be using the second solution you have provided. While using the second one, it is giving me an error "Value of type 'XCUIElementQuery' has no member 'lastMatch'" But it does has a method called firstMatch. If I use firstMatch it is identifying all the Unlock buttons. – Sudheer Apr 18 '22 at 23:03
  • Sorry, lastMatch is a bit of code I’ve written into my Cocoapod that I use everywhere. I often forget it’s not a standard property. You can find the code for it at https://github.com/iammike/XCToolbox/blob/main/XCToolbox/Classes/XCUIElementQuery%2BExtensions.swift. – Mike Collins Apr 19 '22 at 05:39
  • First match will definitely not work as that’ll find the highest level parent that matches and without a doubt include all matches. Last will find the lowest. However, if all these buttons are in the same “otherElement” container (your debug doesn’t show this structure) they’ll all be returned with this method; you’ll have to use indices or provide better identifiers, which is the much better answer. – Mike Collins Apr 19 '22 at 05:42
  • I guess I have to see how I can add accessibility identifiers to resolve this issue. Do. you suggest and documentation for that? Thank you – Sudheer Apr 19 '22 at 15:27
  • If you're using storyboards you can click the element in Xcode, in the right-pane select the "Identity Inspector" menu tab, and under the "Accessibility" header you'll see a text field for "Identifier." – Mike Collins Apr 19 '22 at 17:53
  • If you're using SwiftUI you'll have to dig into the code, identify the views and elements, and add it. `myButton.accessibilityLabel = "LE SR Home notour Button"` – Mike Collins Apr 19 '22 at 17:55