Jamil Rahman
Jamil Rahman

Reputation: 335

How to implement conditional check 'if' an error dialog box is displayed in Espresso

I need to implement a conditional check 'if' an error dialog is existed on the screen in Espresso. The error appears only when a user is currently logged in with another device. If 'OK' button is clicked, it continues.

But in an ideal condition, if the user is not 'logged in with other device' I like to bypass that part of the code in Espresso, I mean, how can I put a condition that, if there is no error box appears, continue normal operation.

Here is the snapshot of the error message and the Espresso code where I am checking the error message's presence and click 'OK' to continue..

enter image description here

...
            //Check if the error message box displayed ?
            ViewInteraction message = onView(
                    allOf(withId(android.R.id.message),
                            withText("You are logged onto another PDT. Click OK to continue/ Utilisateur déjà connecté à un autre TDP. Cliquer OK pour continuer."),
                            childAtPosition(
                                    childAtPosition(
                                            withClassName(is("android.widget.contentPanel")),
                                            0),
                                    0),
                            isDisplayed()));

            //Click on the 'OK' button to continue
            ViewInteraction button2 = onView(
                    allOf(withId(android.R.id.button1),
                            withText("OK"),
                            childAtPosition(
                                    childAtPosition(
                                            withClassName(is("android.widget.LinearLayout")),
                                            0),
                                    2),
                            isDisplayed()));
            button2.perform(click());
....    

Upvotes: 0

Views: 235

Answers (1)

jeprubio
jeprubio

Reputation: 18002

Try wrapping the code you have written between a try / catch block to capture the exception and prevent the test fail:

try {
    //Check if the error message box displayed ?
    ...
    //Click on the 'OK' button to continue
    ...
} catch (Exception e) {
    // The user is not logged anywhere else. Do nothing.
}
// Continue with the rest of the code of the ideal path

This way if the dialog is not visible it will capture the error and the code will continue executing. I did something like this time ago and should work.

Upvotes: 1

Related Questions