PerformException:执行"单击"时出错

Win*_*Hou 47 android android-espresso

我运行android espresso测试时遇到错误:

com.google.android.apps.common.testing.ui.espresso.PerformException:在视图'上执行'单击'时出错,ID为:<2131034173>'.

我的代码很简单:

onView(withId(R.id.btn)).perform(click());
Run Code Online (Sandbox Code Playgroud)

但是这段代码没有错误:

onView(withId(R.id.btn)).check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)

我找不到它为什么会发生的原因.

Ale*_*cha 91

诀窍是读取错误的完整堆栈跟踪.在中间,有一些重要的信息如下:

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
Target view: "ImageView{id=2131492903, res-name=button_hamburger, desc=opens the side drawer, visibility=VISIBLE, width=64, height=64, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=6.0, y=6.0}"
Run Code Online (Sandbox Code Playgroud)

这详细解释了错误.


app*_*oll 29

尽量确保软键盘没有显示.可以使用closeSoftKeyboard ViewAction 轻松关闭它.

此外,请确保禁用系统动画.在设置 - > 开发选项下,关闭以下内容:

  • 窗口动画比例
  • 过渡动画比例
  • 动画师持续时间刻度

此外,这可能是由其他应用程序的ANR对话框引起的.

还有的是报告的问题在这里为好.

  • 谢谢。我已经更改了代码并关闭了所有比例,但错误仍然存​​在。运行测试时没有 ANR 对话框。 (2认同)

Pra*_*ina 22

我遇到了同样的问题,因为软键盘与元素重叠.我使用scrollTo()后跟click()来解决问题.

onView(withId(R.id.btn))
     .perform(scrollTo())
     .perform(click());
Run Code Online (Sandbox Code Playgroud)

如果上述方法无效,请先尝试添加以下内容:

onView(withId(R.id.myEditText)).perform(closeSoftKeyboard());
Run Code Online (Sandbox Code Playgroud)


小智 8

即使使用我也遇到了这个问题

onView(withId(R.id.myEditText)).perform(closeSoftKeyboard());
Run Code Online (Sandbox Code Playgroud)

我发现的是,在我的情况下,在某些设备上,每次我使用

onView(withId(R.id.myEditText)).perform(TypeTextAction());
Run Code Online (Sandbox Code Playgroud)

这就像系统叠在另一个上面一个新的键盘,所以有什么解决我的问题是要始终使用closeSoftKeyboard() 的每次我用TypeTextAction筛选.

onView(withId(R.id.myEditText)).perform(typeTextAction(), closeSoftKeyboard());
Run Code Online (Sandbox Code Playgroud)

因此,如果我需要编辑表单,它将是:

onView(withId(R.id.myEditText1)).perform(typeTextAction(), closeSoftKeyboard());
onView(withId(R.id.myEditText2)).perform(typeTextAction(), closeSoftKeyboard());
onView(withId(R.id.myEditText3)).perform(typeTextAction(), closeSoftKeyboard());
onView(withId(R.id.myEditText4)).perform(typeTextAction(), closeSoftKeyboard());
Run Code Online (Sandbox Code Playgroud)


Kri*_*hna 5

如果在测试期间视图不可见...使用 perform(scrollTo())...它将滚动并执行单击操作。

例子 :-

 onView(withId(R.id.btn)).perform(scrollTo()).perform(click());
Run Code Online (Sandbox Code Playgroud)