Espresso不等待键盘打开

时间:2017-08-08 07:38:44

标签: java android android-espresso

我有一个espresso测试,其中我的屏幕包含EditText和下方的Button跳过。 当我启动活动时,键盘弹出,专注于EditText并重叠Button
我现在想要为跳过按钮编写测试并断言之后发生的事情。

问题是浓缩咖啡不会等待键盘打开 那么会发生什么

  • 意式浓缩咖啡不是在等键盘而是按“跳过”
  • 键盘幻灯片打开
  • 现在键盘下面的某些内容的断言失败

代码如下所示:

public void givenSkipped_whenConfirmed_thenMainActivityLaunched() {
  Espresso.closeSoftKeyboard();// <- Not working as espresso seems to think it is not open yet
  skipPostcodeEntry.perform(click()); //<- Can click this as keyboard is not open yet.

  warningText.check(matches(withText(R.string.some_text)));

  confirmationButton.perform(click());//<- Fails as this is now overlapped by KB

  Assert.DoesSomething()
}

我发现espresso为not waiting for the keyboard to close的问题,但没有等待键盘打开。

有没有人解决过这个问题?

修改

当您查看closeSoftKeyboard方法时,您可以找到名为CloseKeyboardAction的类。您可以看到它甚至在键盘未被识别为打开时记录。

 Log.w(TAG, "Attempting to close soft keyboard, while it is not shown."); 

1 个答案:

答案 0 :(得分:4)

不幸的是,目前似乎Espresso无法检查键盘是否在屏幕上! (https://groups.google.com/forum/#!topic/android-platform/FyjybyM0wGA

作为一种解决方法,我们所做的是检查应该具有焦点的输入字段,然后关闭键盘。这可以防止Espresso在屏幕键盘显示之前调用closeSoftKeyboard()...

@Test
public void testSomething() {
    EspressoExtensions.closeKeyboardOnFocused(fieldThatShouldHaveFocus);
    //Continue with normal test
}

然后将EspressoExtensions添加到您的项目中:

public class EspressoExtensions {
  /**
   * This can be used to close the keyboard on an input field when Android opens the keyboard and
   * selects the first input when launching a screen.
   * <p>
   * This is needed because at the moment Espresso does not wait for the keyboard to open
   */
  public static void closeKeyboardOnFocused(ViewInteraction viewInteraction) {
    viewInteraction.check(matches(hasFocus())).perform(closeSoftKeyboard());
  }
}

希望这会有所帮助,直到Espresso有办法断言键盘是否在屏幕上