如何在Espresso测试中点击快餐栏按钮?

时间:2015-10-28 01:25:15

标签: java android android-espresso android-snackbar

我不相信这是一个愚蠢的问题。我正在写一个简单的Espresso测试,其中一部分涉及点击" Ok"小吃店里的按钮。

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:不会执行Action,因为   目标视图与以下一个或多个约束不匹配:   至少90%的视图区域显示给用户。   目标视图:" AppCompatButton {id = 2131558552,res-name = snackbar_action,   visibility = VISIBLE,width = 264,height = 144,has-focus = false,   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}"

有什么想法吗?

2 个答案:

答案 0 :(得分:8)

此处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的{​​{1}}中只有不到90%可用于Espresso框架click()。您可以按照The Espresso Setup Instructions

中的建议,通过停用动画来解决此问题
  • 导航至您的手机Developer Options
  • 设置以下内容
    • 窗口动画比例= 0.0x
    • 过渡动画比例= 0.0x
    • 动画师持续时间刻度= 0.0x

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

答案 1 :(得分:2)

只需使用ID snackbar_action

即可找到它
onView(allOf(withId(android.support.design.R.id.snackbar_action)))
    .perform(click());