Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an accessibility identifier properly in SwiftUI?

I would like to be able to tap a concrete button on my stack of views. To achieve this, first I set on my code an accessibility id:

Button(action: {
                 withAnimation(.easeOut(duration: 1)) {
                    self.modalShown.toggle()
                 }

                }) {
                      FilterButton()
                     .accessibility(identifier: "modalFilterViewBtn")
                    }
                    .buttonStyle(PlainButtonStyle())
                }

So, then on my text case I tried to look for that id:

IOSElement elem1 = driver.findElementByAccessibilityId("modalFilterViewBtn");
    wait.until(ExpectedConditions.elementToBeClickable(elem1));
    elem1.click();

And I obtain as a result: An element could not be located on the page using the given search parameters.(..)

Finally if I use the Appium inspector to know the accessibility id of my desired element, it seems to be another name that is not the same that I had set in code:

enter image description here

Even if I look for the element with that id("Filter"), it also can not be found during my test case.

So, what is the correct way to set this accessibility id using SwiftUI?

Thank you

like image 535
Andoni Da Silva Avatar asked Dec 22 '22 18:12

Andoni Da Silva


1 Answers

I assume it should be like

Button(action: {
    // action here
}) {
    // button label view here
}
.buttonStyle(PlainButtonStyle())
.accessibility(identifier: "modalFilterViewBtn")

Note in iOS 14+ there is .accessibilityIdentifier(_ identifier: String) instead. accessibility(identifier: String) is deprecated.

like image 159
Asperi Avatar answered Mar 07 '23 12:03

Asperi