Testing Login with espresso is throwing exception

315
August 29, 2017, at 7:53 PM

I am trying to test the text of the button is changing while trying to login when the button is pressed. Login button text is actually changing the text to Verifying... when the system is checking the credentials with server and once done, the text changes to LOGIN again. Whenever I am trying to test that with espresso;the UI part is completing assigning values to the edittext and clicked the button, the thread then freezes and few times later it throws an error. As I am new in testing, I would be grateful if you could explain how this problem can be resolved or what is the approach that I should take for this kind of scenarios.

This is my test class.

@LargeTest
@RunWith(AndroidJUnit4.class)
public class LoginTest {
 private String userName;
 private String userPass;
 private LoginIdlingResource idlingResource;
 @Rule
 public ActivityTestRule<LoginActivity> activityRule = new 
 ActivityTestRule<LoginActivity>(LoginActivity.class);
 @Before
 public void assignCredentials (){
    userName = "ABC";
    userPass = "ABC";
 }
 @Before
 public void registerIntentServiceIdlingResource() throws Exception {
    LoginActivity activity = activityRule.getActivity();
    idlingResource = new LoginIdlingResource(activity);
    Espresso.registerIdlingResources(idlingResource);
 }
 @Test
 public void buttonTextChanged(){
    onView(withId(R.id.edittext_user))
            .perform(typeText(userName));
    onView(withId(R.id.edittext_pass))
            .perform(typeText(userPass));
    onView(withId(R.id.submit_login))
            .perform(click());
    onView(withId(R.id.submit_login))
            .check(matches(withText("Verifying...")));
 }
 @After
 public void unregisterIntentServiceIdlingResource() {
    Espresso.unregisterIdlingResources(idlingResource);
 }
}

I have prepared an IdlingResource.

public class LoginIdlingResource implements IdlingResource {
private LoginActivity mActivity;
private ResourceCallback mCallBack;
 public LoginIdlingResource(LoginActivity context){
    mActivity = context;
 }
 @Override
 public String getName() {
    return "LoginActivityIdleName";
 }
 @Override
 public boolean isIdleNow() {
    boolean idle = isIdle();
    if (idle && mCallBack!=null){
        mCallBack.onTransitionToIdle();
    }
    return idle;
 }
 @Override
 public void registerIdleTransitionCallback(ResourceCallback callback) {
    this.mCallBack = callback;
 }
 public boolean isIdle() {
    return mActivity != null && mCallBack != null && 
    !mActivity.isNetworkOperationGoingOn();
 }
}

This is generating the following exception

android.support.test.espresso.PerformException: Error performing 'single 
click - At Coordinates: 539, 903 and precision: 16, 16' on view 'with id: 
com.abc.rt:id/submit_login'.
at android.support.test.espresso.PerformException$Builder.
build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.
getUserFriendlyError(DefaultFailureHandler.java:80)
at android.support.test.espresso.base.DefaultFailureHandler.
handle(DefaultFailureHandler.java:56)
at 
android.support.test.espresso.ViewInteraction.
runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.
doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.
perform(ViewInteraction.java:87)
at com.abc.rt.LoginTest.buttonTextChanged(LoginTest.java:92)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.
runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.
run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.
invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.
evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.
evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.
evaluate(RunAfters.java:27)
at android.support.test.internal.statement.UiThreadStatement.
evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.
evaluate(ActivityTestRule.java:270)
Answer 1

Try registering your IdlingResource after you click the button:

// remove @Before
@Test
public void buttonTextChanged(){
   onView(withId(R.id.edittext_user))
           .perform(typeText(userName));
   onView(withId(R.id.edittext_pass))
           .perform(typeText(userPass));
   onView(withId(R.id.submit_login))
           .perform(click());
   LoginIdlingResource idlingResource = new LoginIdlingResource(activityRule.getActivity());
   Espresso.registerIdlingResources(idlingResource);
   onView(withId(R.id.submit_login))
           .check(matches(withText("Verifying...")));
   Espresso.unregisterIdlingResources(idlingResource);
}
// remove @After
Rent Charter Buses Company
READ ALSO
Firebase Cloud Messaging With AWS SNS

Firebase Cloud Messaging With AWS SNS

I am trying to integrate FCM with SNSI read in this answer that the setup should be identical with GCM

532
Converting SVG into Raster PNGs by Android Studio

Converting SVG into Raster PNGs by Android Studio

I am working with Android Studio 22

335
Which app controls Yes/No Authentication in Android?

Which app controls Yes/No Authentication in Android?

Google has recently updated its authentication mechanism and occasionally asks whether you are loging in (Yes / No) rather than request a code

193