google / EarlGrey

:tea: iOS UI Automation Test Framework
http://google.github.io/EarlGrey/
Apache License 2.0
5.61k stars 740 forks source link

UIAlertController.StyleAlert button duplicity wrinkle #271

Open gurgeous opened 7 years ago

gurgeous commented 7 years ago

I can't easily test UIAlertController.StyleAlert because each button has two accessible views with the same label:

Exception: MultipleElementsFoundException
Reason: Action 'Tap' was not performed because multiple UI elements matching ((respondsToSelector(isAccessibilityElement) && isAccessibilityElement) && accessibilityLabel("Download")) were found. Use grey_allOf(...) to create a more specific matcher.
Complete Error: Multiple elements were matched: (
    "<_UIAlertControllerActionView:0x7f9df3435eb0; AX=Y; AX.label='Download'; AX.frame={{188, 352}, {134.5, 44}}; AX.activationPoint={255.25, 374}; AX.traits='UIAccessibilityTraitButton'; AX.focused='N'; frame={{0, 0}, {134.5, 44}}; opaque; alpha=1>",
    "<UILabel:0x7f9df34369f0; AX=Y; AX.label='Download'; AX.frame={{216, 364}, {78.5, 20.5}}; AX.activationPoint={255.25, 374.25}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{0, 12}, {78.5, 20.5}}; opaque; alpha=1; UIE=N; text='Download'>"
). Please use selection matchers to narrow the selection down to single element.

It's easy to workaround the issue with grey_allOf, but this is a common case that should be handled by the framework in my opinion.

tirodkar commented 7 years ago

Hmm, I wonder if that might be an issue when considering tapping on custom UIAlertControllers that have their own views in the buttons or so. BTW, are you able to tap on the alert using grey_text(@"Download")? as in here

gurgeous commented 7 years ago

grey_text works great, thanks! This is the first I heard of it, though, and I've been perusing the project for a few weeks now. Is it documented somewhere?

tirodkar commented 7 years ago

It isn't documented anywhere per se, it's a part of disambiguating the matchers. Notice how in the trace,

"<_UIAlertControllerActionView:0x7f9df3435eb0; AX=Y; AX.label='Download'; AX.frame={{188, 352}, {134.5, 44}}; AX.activationPoint={255.25, 374}; AX.traits='UIAccessibilityTraitButton'; AX.focused='N'; frame={{0, 0}, {134.5, 44}}; opaque; alpha=1>",
"<UILabel:0x7f9df34369f0; AX=Y; AX.label='Download'; AX.frame={{216, 364}, {78.5, 20.5}}; AX.activationPoint={255.25, 374.25}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{0, 12}, {78.5, 20.5}}; opaque; alpha=1; UIE=N; text='Download'>"

The text='Download' was what separated the two. We try to provide matchers that can help identify and pinpoint these differing characteristics.

bootstraponline commented 7 years ago

Is it documented somewhere?

Yes, it's documented in the cheatsheet

gurgeous commented 7 years ago

Just listing the method name doesn't count! :)

If this is the best way to find buttons, it should be more prominent in the docs.

tirodkar commented 7 years ago

@gurgeous, agreed :). We have the Selection API section in our docs to cover this. If you feel it's not clear enough when it comes to pointing to grey_text(), then feel free to add to it or provide a suggestion on how to make it more clear to users.