Reputation:
What I wish to do is to check if a specific DialogFragment
is visible and only if its visible I want to execute a code like this onView(withId(R.id.start_app)).perform(click());
Right now I have this code which I cant use in a if-condition.
Both codes in this method will run regardless of if the Dialogfragment
is visible or not.`
@Test
public void testHowDoIBecomeCustomerButtonX() {
onView(withId(R.id.cancel_button))
.inRoot(isDialog())
.check(matches(isDisplayed()));
onView(withId(R.id.becoming_customer)).perform(click());
}
Upvotes: 2
Views: 1602
Reputation: 3809
What you can do is wrap your condition into a try-catch
where the condition throws an exception if not satisfied and therefore leaving the try
block.
It could look something like this:
try {
onView(withId(R.id.cancel_button)).check(matches(isDisplayed()));
onView(withId(R.id.becoming_customer)).perform(click());
} catch (NoMatchingViewException e) {
}
If the first check for the cancel_button
fails the second line is not executed. You just have to make sure your first condition is evaluating correctly.
But generally speaking this is a bad pattern. There is a reason why this is not possible without using some kind of hack. You should avoid conditions in your tests at all! It would be much better to have two separate test cases where in one test case the dialog appears and you go on with your tests and in a second one it does not appear and you go on from there.
Upvotes: 2