【问题标题】:How to click the snackbar button in Espresso testing?如何在 Espresso 测试中单击快餐栏按钮?
【发布时间】:2016-01-27 15:42:54
【问题描述】:

我不认为这是一个愚蠢的问题。我正在编写一个简单的 Espresso 测试,其中一部分涉及单击快餐栏中的“确定”按钮。

Espresso.onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(R.string.permission_snackbar)))
            .check(matches(isDisplayed()));
Espresso.onView(withText("Ok")).perform(click());

这会抛出

android.support.test.espresso.PerformException:执行错误 '单击'视图'带有文本:是“确定”'。造成的: java.lang.RuntimeException:将不会执行操作,因为 目标视图与以下一项或多项约束不匹配: 至少 90% 的视图区域显示给用户。 目标视图:“AppCompatButton{id=2131558552, res-name=snackbar_action, 可见性=可见,宽度=264,高度=144,有焦点=假, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=684.0, y=53.0, text=Ok, input-type=0, ime-target=false, has-links=false}"

有什么想法吗?

【问题讨论】:

  • “我不认为这是一个骗人的问题。”当然不是,从来没有人这样做过。如果您要解释为什么这不是骗局,请给出具体原因为什么现有的具体问题没有回答您的问题;否则,甚至不要提出来。
  • 我也有差不多的问题,你是怎么解决的?

标签: java android android-espresso android-snackbar


【解决方案1】:

RuntimeException 在这里看到 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.。实际上,问题确实来自View 未能完全显示。

问题是由竞争条件引起的。您正试图打开视图,并且在它打开期间您正试图单击它。如果时机不对,那么View 的不到 90% 将可用于 Espresso 框架到click()。按照The Espresso Setup Instructions中的建议,您可以通过禁用动画来解决此问题

  • 导航到您手机的Developer Options
  • 设置以下
    • 窗口动画比例 = 0.0x
    • 过渡动画比例 = 0.0x
    • 动画持续时间比例 = 0.0x

我自己的测试表明,您只需将Transition Animation Scale 设置为0.0x 即可。正如您可以想象的那样,对于您遇到的竞争条件,这是一个完全一致的解决方案。

【讨论】:

    【解决方案2】:

    使用 id snackbar_action 可以很容易地找到它:

    onView(allOf(withId(android.support.design.R.id.snackbar_action)))
        .perform(click());
    

    【讨论】:

    • 如果您仔细查看 OP,您会发现匹配器工作正常。我的回答讨论了导致此问题的竞争条件。
    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2019-07-09
    • 2017-07-29
    相关资源
    最近更新 更多