简体   繁体   中英

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,ie,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 ie, 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());
}

There is a keyboard setting for showing the virtual keyboard even when typing is performed via a hardware keyboard (which the tests pretend to use). It can be set via ADB

adb shell settings put secure show_ime_with_hard_keyboard 1

or accessed in the navigation bar when the cursor is in a text field:

导航栏中的键盘设置

Then set the "Show virtual keyboard" option:

显示虚拟键盘设置

This should show the keyboard while in the text field, but it will automatically close once the focus leaves the text field (which is not the case for real phones).

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM