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

use*_*ser 9 java android android-espresso android-snackbar

我不相信这是一个愚蠢的问题.我正在写一个简单的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());
Run Code Online (Sandbox Code Playgroud)

这引发了

android.support.test.espresso.PerformException:在带有text的视图'上执行'单击'时出错:是"Ok"'.引发者:java.lang.RuntimeException:将不执行操作,因为目标视图与以下一个或多个约束不匹配:至少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}"

有任何想法吗?

OYR*_*YRM 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完全显示的失败.

问题是由竞争条件引起的.您正在尝试打开视图,并在打开视图时尝试单击它.如果时机不对,那么ViewEspresso框架可用的比例不到90%click().您可以按照"浓咖啡设置说明"中的建议,通过禁用动画来解决问题

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

我自己的测试表明您可以只设置脱身Transition Animation Scale0.0x.您可以想象,这是您遇到的竞争条件的完美一致的解决方案.


Rém*_*i F 4

可以简单地使用 id 找到它snackbar_action

onView(allOf(withId(android.support.design.R.id.snackbar_action)))
    .perform(click());
Run Code Online (Sandbox Code Playgroud)