0

I found on Stackoverflow something like that about checking when dialog is visible:

 onView(withText("Yes"))
                    .inRoot(isDialog())
                    .check(matches(isDisplayed()))
                    .perform(click());

Of course this works if Dialog with button 'yes' is visible, but in different scenario, if dialog will be invisible I got crash:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "Yes"

So how to write that if the dialog exists, click yes, and if it does not exist, then nothing will be clicked?

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
purcha
  • 371
  • 3
  • 12

1 Answers1

2

You could try this:

  onView(withText("Yes")).inRoot(isDialog()).withFailureHandler(new FailureHandler() {
            @Override
            public void handle(Throwable error, Matcher<View> viewMatcher){

            }
        }).check(matches(isDisplayed())).perform(customClick());

//if dialog is visible, perform click, otherwise do nothing.
navylover
  • 12,383
  • 5
  • 28
  • 41