>我有一个浓缩咖啡测试,我的屏幕包含一个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()
}
我发现了一个问题,即浓缩咖啡没有等待键盘关闭,但没有等待键盘打开。
有没有人解决这个问题?
编辑:
当您查看closeSoftKeyboard
方法时,您可以找到一个名为CloseKeyboardAction
的类。您可以看到,当键盘未被识别为打开时,它甚至会记录。
Log.w(TAG, "Attempting to close soft keyboard, while it is not shown.");
不幸的是,目前似乎Espresso无法检查键盘是否在屏幕上!(https://groups.google.com/forum/#!topic/android-platform/FyjybyM0wGA(
作为一种解决方法,我们要做的是检查应该具有焦点的输入字段,然后关闭键盘。这可以防止浓缩咖啡在键盘在屏幕上之前调用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有办法断言键盘是否在屏幕上