dimanche 8 mai 2016

Android - Soft keyboard not appearing during Espresso test

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 ?

Aucun commentaire:

Enregistrer un commentaire