【问题标题】:Android - Soft keyboard not appearing during Espresso testAndroid - 在 Espresso 测试期间未出现软键盘
【发布时间】:2016-05-09 05:25:53
【问题描述】:

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

 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 测试时,我看不到用于在电子邮件编辑文本中输入值的软键盘。这里我提到了我的 Espresso Ui 测试用例代码。

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

【问题讨论】:

  • 你定义IntentTestRule了吗?

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


【解决方案1】:

即使通过硬件键盘(测试假装使用)执行打字,也有显示虚拟键盘的键盘设置。 可以通过ADB设置

adb shell settings put secure show_ime_with_hard_keyboard 1

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

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

这应该在文本字段中显示键盘,但是一旦焦点离开文本字段,它将自动关闭(对于真实手机而言并非如此)。

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 能分享一下你写过UI测试用例的课吗?
    • 你能看到我上面的 SampleTest 课程吗?
    猜你喜欢
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多