ir2pid
ir2pid

Reputation: 6126

Android espresso multiple test paths

How do I have an if/else path in my tests

for example

when a certain dialog is present I dismiss it and continue

vs

when it's not present I continue nonetheless

Upvotes: 0

Views: 121

Answers (1)

mark w.
mark w.

Reputation: 1097

What you need is something like an isVisible(int id), the implementation of which would be something like this:

public boolean isVisible(int elementID) {
        try {
            onView(withId(elementID)).check(matches(isDisplayed()));
            return true;
        } catch (Throwable t) {
            return false;
        }
    }

You would then check for the dialog in your test like this:

if(isVisible(R.id.dialogID)) {
    onView(withText("OK")).perform(click()); // dismiss the dialog by clicking 'OK' button
    // do whatever you want to do after this
}

This should take care of your problem.

Upvotes: 1

Related Questions