有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Espresso并没有等待键盘打开

我有一个浓缩咖啡测试,我的屏幕下面有一个EditText和一个skipButton。 当我启动活动时,键盘弹出打开,集中在EditText上,并与Button重叠
现在我想为skip按钮编写一个测试,并断言之后会发生什么

问题在于,浓缩咖啡不会等待键盘打开
所以发生的是

  • Espresso没有等待键盘并按下“跳过”
  • 键盘滑开
  • 对现在位于键盘下方的内容的断言失败

代码如下所示:

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

我发现了一个问题,意式浓缩咖啡是not waiting for the keyboard to close的,但没有什么关于它不等待键盘打开

有人解决了这个问题吗

编辑

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

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

共 (1) 个答案

  1. # 1 楼答案

    不幸的是,目前看来浓缩咖啡无法检查键盘是否在屏幕上!(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());
      }
    }
    

    希望这能有所帮助,直到浓缩咖啡能够判断键盘是否在屏幕上