我无法使ViewActions.closeSoftKeyboard()在Espresso 2.2.2中运行

get*_*zeg 15 testing android unit-testing android-espresso

我正在尝试测试应用程序,我需要隐藏键盘,因为我无法点击按钮.所以,我在build.gradle中添加了Espresso:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
Run Code Online (Sandbox Code Playgroud)

并试图从android.support.test.espresso.action.ViewActions库中使用它:

ViewActions.closeSoftKeyboard();
Run Code Online (Sandbox Code Playgroud)

测试成功运行,但在我的布局中的EditText中键入一些文本后失败.键盘仍在那里,显示出来.

PS我在读完这个答案后意识到这是键盘故障.

Be_*_*ive 35

ViewAction除非在它中使用,否则它本身不会做任何事情ViewInteraction.这意味着您需要使用之前的操作将其链接到perform()这样:onView()..perform(typeText(..), closeSoftKeyboard())或者使用类中的内置帮助程序Espresso:Espresso.closeSoftKeyboard()

  • 他们确实应该给这些起一个不同的名字。导入后,并不是很明显,只有当链接到ViewActions时,名为“ closeSoftKeyboard()”的方法才有效。我被这个吓到了,但是你的回答帮助了我。从现在开始,我将使用Espresso.closeSoftKeyboard()明确显示。那就是他们在示例中所做的,直到现在我才明白为什么:) (2认同)

小智 9

你可以这样实现:

fun hideKeyboard() {
    onView(ViewMatchers.isRoot()).perform(ViewActions.closeSoftKeyboard())
  }
Run Code Online (Sandbox Code Playgroud)

之后只使用这个:paymentMethodPage.hideKeyboard()