In Espresso we create the test scripts for a particular scenario like “login to checkout” in which user will log in then the user will place order with his product.
But if the user already logged in, then our script will throw error at the beginning of the script.
So I have the solution for this, in which it will check whether the user already logged in or not , then it will perform action on the basis of logged in state and not logged in state.
We will check that the user logged in or not on the basis of following :-
1 |
AppSharedPref.isLoggedIn(mActivityTestRule.getActivity()); |
Now we will apply the condition of “if ” and “else” on the basis of that code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
if (AppSharedPref.isLoggedIn(mActivityTestRule.getActivity())) { ViewInteraction appCompatImageView = onView( allOf(withId(R.id.button_id), childAtPosition( childAtPosition( withClassName(is("android.widget.LinearLayout")), 0), 0), isDisplayed())); appCompatImageView.perform(click()); } else { ViewInteraction appCompatImageView = onView( allOf(withId(R.id.button_id), childAtPosition( childAtPosition( withClassName(is("android.widget.LinearLayout")), 0), 0), isDisplayed())); appCompatImageView.perform(click()); ViewInteraction appCompatTextView = onView( allOf(withText("Login"), childAtPosition( childAtPosition( withClassName(is("android.support.v7.widget.LinearLayoutCompat")), 3), 2), isDisplayed())); appCompatTextView.perform(click()); } |
In the following code, we are checking that whether user logged in or not .
If the user is already logged in then “if” condition will be executed otherwise “else” condition will be executed by performing the action after logging in.
Hope this blog will help you.