Android - 在Espresso测试期间不出现软键盘

时间:2016-05-09 05:25:53

标签: android user-interface testing android-softkeyboard android-espresso

我有登录屏幕,其中包含要输入的电子邮件和密码,底部有提交按钮。根据要求,无论何时启用软键盘,我都会将提交按钮移动到顶部,即向用户显示提交按钮使用以下代码在软键盘上方和我的电子邮件/密码布局下方。

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

运行我的应用程序时,每件事都运行正常。但问题是在运行espresso UI测试时,我无法看到用于在电子邮件edittext中输入值的软键盘。在这里,我提到了我的Espresso Ui测试用例代码。

public class LoginActivityTest {     @规则     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());
}

***注意:如果我为我的布局评论全局布局监听器,即我的活动中的mainrel,那么我能够执行测试用例。我认为使用OnGlobalLayoutListener我的按钮移动有问题。

任何人都可以建议/帮助我吗?

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

2 个答案:

答案 0 :(得分:2)

即使通过硬件键盘(测试假装使用)进行键入,也有键盘设置用于显示虚拟键盘。 它可以通过ADB设置

adb shell settings put secure show_ime_with_hard_keyboard 1

或当光标位于文本字段时在导航栏中访问:

Keyboard settings in navigation bar

然后设置“显示虚拟键盘”选项:

Show virtual keyboard setting

这应该在文本字段中显示键盘,但是一旦焦点离开文本字段,它就会自动关闭(实际手机不是这种情况)。

答案 1 :(得分:0)

试试这个:

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