Issue
I have two activities on my app: a login activity (loginActivity) and a second activity (mainActivity). I want to use Espresso to test the login on the loginActivity, so I wrote this test:
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
public LoginActivityTest() {
super(LoginActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testLogin() throws Exception {
onView(withId(R.id.button_log_in)).perform(click());
onView(withId(R.id.container)).check(matches(isDisplayed()));
}
}
The problem is that when the app starts, if the user had previously logged in, the loginActivity immediately tart the mainActivity, and when the test is performed it fails with the error:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.test.android.development:id/R.id.button_log_in
Note: if I start the app and do a log out before I run the test, the error disappears.
Thanks in advance!
Solution
Use an if
to perform the test only when the user hasn't previously logged in.
Then you can add an else
to also test the logged status.
Otherwise, you have to try and catch the NoMatchingViewException to tell your app not to perform the login test in case of logged user. Something like:
try {
// your test code here
} catch (NoMatchingViewException e){
Log.d(TAG_LOG, "No login test performed!");
}
Let me know if this was of any help :)
Answered By - Clover
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.