【问题标题】:PerformException: Error performing 'single click'PerformException:执行“单击”时出错
【发布时间】:2020-12-26 19:49:46
【问题描述】:

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

com.google.android.apps.common.testing.ui.espresso.PerformException:在视图“id 为 ”上执行“单击”时出错。

我的代码很简单:

onView(withId(R.id.btn)).perform(click());

但是这段代码没有错误:

onView(withId(R.id.btn)).check(matches(isDisplayed()));

我找不到它发生的原因。

【问题讨论】:

  • 请发布完整的堆栈跟踪。错误消息的第一位是不够的。使用 PerformExceptions,有趣的位在“Caused by”部分,它显示了最初导致单击失败的异常。
  • @Winton 你找到解决这个问题的方法了吗?我遇到了同样的问题
  • 这可能在您运行一批测试时发生。尝试再次运行相同的测试(单独测试),看看您是否仍然面临同样的问题。

标签: android android-espresso


【解决方案1】:

诀窍是读取错误的完整堆栈跟踪。在中间,有一些关键的信息是这样的:

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}"

详细解释了错误。

【讨论】:

  • 谢谢!就我而言,这是因为 NPE。这很奇怪,因为我希望我的变量被实例化。看来我必须运行整个测试,以便在运行之前初始化 Activity/Fragment。我正在运行一个测试方法(描述一个用户故事)。我强烈建议谷歌删除测试模式旁边的播放按钮,因为在这种情况下它是无用的,我认为它在其他情况下也无用。 ://
  • 谢谢!就我而言,我未能将应该在测试中启动的活动添加到清单文件中。
  • 这正是我的情况,谢谢!
  • 阅读整个堆栈跟踪的非常有用的建议 - 我在另一个线程中有 NPE,导致点击问题,从主消息中几乎无法得知。
  • 对我来说这是因为我在测试期间一直在玩whatsapp,这并不奇怪!
【解决方案2】:

尝试确保没有显示软键盘。它可以通过 closeSoftKeyboard ViewAction 轻松关闭。

此外,请确保禁用系统动画。在 Settings -> Developing Options 下关闭以下选项:

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

另外,这可能是由其他应用的 ANR 对话框引起的。

here 也报告了一个问题。

【讨论】:

  • 谢谢。我更改了代码并关闭了所有比例,但错误仍然存​​在。运行测试时没有 ANR 对话框。
  • 很好的答案。 Espresso.closeSoftKeyboard() 拯救我的一天......因为我的测试第二个测试失败(无论顺序如何),当第一个和第二个测试都有一个按钮可以点击时..
【解决方案3】:

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

onView(withId(R.id.btn))
     .perform(scrollTo())
     .perform(click());

如果上述方法不起作用,请先尝试添加以下内容:

onView(withId(R.id.myEditText)).perform(closeSoftKeyboard());

【讨论】:

【解决方案4】:

我也遇到过这个问题

onView(withId(R.id.myEditText)).perform(closeSoftKeyboard());

我发现,就我而言,在某些设备上,每次我都使用

onView(withId(R.id.myEditText)).perform(TypeTextAction());

就像系统将一个新键盘堆叠在另一个之上,所以解决我的问题的方法是始终使用 closeSoftKeyboard() EVERY-TIME 我是这样使用 TypeTextAction 的。

onView(withId(R.id.myEditText)).perform(typeTextAction(), closeSoftKeyboard());

所以如果我需要编辑一个表单,它会是这样的:

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

【讨论】:

  • 我收到了Unresolved reference: typeTextAction
【解决方案5】:

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

示例:-

 onView(withId(R.id.btn)).perform(scrollTo()).perform(click());

【讨论】:

    【解决方案6】:

    使用编辑文本执行 typeText 后,关闭软键盘,因为它可能会遮住您的视野

    closeSoftKeyboard()

    所以完整的代码是:

        onView(withId(R.id.fab)).perform(click())
        onView(withId(R.id.edReminder)).perform(typeText("TestAA"))
        closeSoftKeyboard()
        onView(withId(R.id.btnAdd)).perform(click())
    

    注意:不允许使用 closeSoftKeyboard()perform(),因为 perform() 接受 ViewAction 和 closeSoftKeyboard() 返回 Unit 使用ViewActions.closeSoftKeyboard()perform()

    【讨论】:

      【解决方案7】:

      您的 TestCase 中的一种可能性是,如果您使用 LiveData 执行数据库操作,那么您应该避免使用以下规则。

      @规则 public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();

      删除这些行后,TestCase 可以完美运行。

      【讨论】:

        【解决方案8】:

        我遇到了同样的问题,通过改变元素的位置解决了。

        我试图点击的位置没有元素。试图点击位置 3 但元素位于第二位置(完全忘记索引从 0 开始) 所以,我改变了元素的位置,它现在可以正常工作了

        【讨论】:

          【解决方案9】:

          发生这种情况的原因很少。在我的情况下,这是因为单击按钮后有一个progress bar 会永远旋转,所以请确保是否有网络调用或一些等待过程,在收到回调后停止进度条。 执行点击也会等待某些操作发生,因此请确保您不仅在点击,而且在执行点击时还执行操作。

          【讨论】:

            【解决方案10】:

            对我来说,就像 Eric Aya 所说的那样。但我并没有每次都关闭键盘,只是在我需要“更换键盘”之前和之后,因为我正在输入一个数字 Edittext。

                //Type the user data
                onView(withId(R.id.edit_name)).perform(typeText(name));
                onView(withId(R.id.edit_lastname)).perform(typeText(lastname));
                onView(withId(R.id.edit_email2)).perform(typeText(email));
                onView(withId(R.id.edit_password2)).perform(typeText(password), closeSoftKeyboard());
            
                //if its a number edittext we have to use String.valueOf
                //also we need to closesoftkeyboard before and after, so it changes from text 
                //to number
                onView(withId(R.id.edit_age)).perform(typeText(String.valueOf(age)), 
                closeSoftKeyboard());
                onView(withId(R.id.edit_is_admin)).perform(typeText(admin), closeSoftKeyboard());
            

            【讨论】:

              【解决方案11】:

              error 是由UI 线程阻塞引起的。请检查您的target Activity code,尤其是setUpinit 函数。

              我遇到了同样的error,在 UI 线程中有一个错误的listener 总是被调用。当我删除listener 时,error 可以修复。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2019-01-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-03-26
                相关资源
                最近更新 更多