Android - Soft keyboard does not appear during the Espresso test

advertisements

I have Login screen , which contains email and password to be enter with submit button at the bottom.As per the requirement whenever soft keyboard is enabled , i am moving the submit button to top,i.e,showing submit button to the user above soft keyboard and below my email/password layout using the below code.

 mainrel.getViewTreeObserver().addOnGlobalLayoutListener(new    ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
          Rect r = new Rect();
          mainrel.getWindowVisibleDisplayFrame(r);
          int heightDiff = mainrel.getRootView().getHeight() - (r.bottom - r.top);
          if (heightDiff > 128 && heightDiff != 146) {
              //KeyBoard Enabled
              moveButtonTo_Top();
          } else {
              //KeyBoard Disabled
              moveButtonTo_Down();
          }
      }
  });
  //Floating down the button
private void moveButtonTo_Down() {
    RelativeLayout.LayoutParams relativeparams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    relativeparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    relativeparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    relativeparams.setMargins(0, 0, 0, Constants.NEXT_LAYOUT_MARGIN_TOP);
    submitLay.setLayoutParams(relativeparams);
}

//Floating Up button
private void moveButtonTo_Top() {
    RelativeLayout.LayoutParams relativeparams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    relativeparams.addRule(RelativeLayout.BELOW, R.id.mainLinearlay);
    relativeparams.setMargins(0, 20, 0, 0);
    relativeparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    submitLay.setLayoutParams(relativeparams);
}

Every thing works fine while running my application.But the problem is while running espresso UI tests i am not able to see the softkeyboard for entering values in email edittext. Here i am mentioning my Espresso Ui test case code.

public class LoginActivityTest { @Rule public ActivityTestRule mActivityRule = new ActivityTestRule(LoginActivity.class);

@Test
public void login() {
    onView(withId(R.id.ripplebtn_step)).perform(click());
    Utils.sleep(3000);
    //Sign In
    onView(withId(R.id.edt_email)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_USER_EMAIL)).perform(closeSoftKeyboard());
    onView(withId(R.id.edt_password)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_PASSWORD)).perform(closeSoftKeyboard());
    onView(withId(R.id.txt_submit)).perform(click());
}

*** Note : If i comment global layout listener for my layout i.e, mainrel in my activity , then i am able to execute the test case. I think there is problem with my button movement to up and down using OnGlobalLayoutListener.

Can anyone please suggest / help me ?

 public class SampleTest {
@Rule
public IntentsTestRule<StepSignInActivity> mAddIntentsTestRule =
        new IntentsTestRule<StepSignInActivity>(SignInActivity.class);
IdlingResource idlingResource;

@Before
public void before() {
    idlingResource = new ElapsedTimeIdlingResourc`enter code here`e(5000);
    Espresso.registerIdlingResources(idlingResource);
}
@After
public void after() {
    Espresso.unregisterIdlingResources(idlingResource);
}

@Test
public void runSequence() {
    // this triggers our intent service, as we registered
    // Espresso for it, Espresso wait for it to finish
    onView(withId(R.id.edt_email)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_USER_EMAIL));
    onView(withId(R.id.edt_password)).perform(typeText(TestCaseConstants.CUSTOM_SIGNIN_PASSWORD)).perform(closeSoftKeyboard());
    onView(withId(R.id.txt_submit)).perform(click());
}


Try This:

@Rule
public IntentsTestRule<YourActivity> mAddIntentsTestRule =
        new IntentsTestRule<>(YourActivity.class)

@Before
public void registerIdlingResource() {
    Espresso.registerIdlingResources(
            mAddIntentsTestRule.getActivity().getCountingIdlingResource());
}

 /**
 * Unregister your Idling Resource so it can be garbage collected and does not leak any memory.
 */
@After
public void unregisterIdlingResource() {
    Espresso.unregisterIdlingResources(
            mAddIntentsTestRule.getActivity().getCountingIdlingResource());
}