I have the following case. We use the Gherkin language to control our own ui automation using Espresso. In our Gherkin lines, we have a line called:
And I tap on button "Next"
Where "Next" is the String variable, we go to our glue code (we use the Cucumber structure).
As it happens, our application has many "Next" buttons with different resource identifiers. I ended up using gherkin lines with variables like:
And I tap on button "Next in screen 1"
And I tap on button "Next in screen 2"
Now I want to use only the Gherkin "Next" variable in my code. I get an Integer array that contains all the resource identifiers from all the Next buttons, but when I want to check which identifier is displayed on the screen, I got a NoMatchingViewException.
This is my current solution:
final int[] uiElementIds = getArrayWithIdsFromUIElement("Next");
int testId = 0;
for(int id : uiElementIds) {
try {
onView(withId(id)).check(matches(isDisplayed()));
testId = id;
break;
} catch(NoMatchingViewException ex) {
}
}
final int uiElementId = testId;
onView(withId(uiElementId)).withFailureHandler(new FailureHandler() {
@Override
public void handle(Throwable error, Matcher<View> viewMatcher) {
onView(withId(uiElementId)).perform(scrollTo(), click());
}
}).perform(click());
As you can see, I just catch the NoMatchingViewException that is thrown and do nothing with it until I find the correct identifier and exit the for loop.
My question is:
Is there a better approach we can use to scroll it to see which identifier is displayed, and if so, click on it?
In my mind, I came up with this idea, but it is not integrated into Espresso:
for(int id : uiElementIds) {
if(onView(withId(id)).exist()) {
performClick();
}
}