java - 如何在 Espresso 测试中点击 snackbar 按钮?

标签 java android android-espresso android-snackbar

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

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: Error performing 'single click' on view 'with text: is "Ok"'. 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: "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}"

有什么想法吗?

最佳答案

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区域显示给用户。。事实上,问题确实来自于 View 无法完全显示。

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

  • 导航到您手机的开发者选项
  • 设置以下内容
    • 窗口动画比例 = 0.0x
    • 过渡动画比例 = 0.0x
    • 动画师持续时间比例 = 0.0x

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

关于java - 如何在 Espresso 测试中点击 snackbar 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33381366/

相关文章:

java - 这些陈述是等价的吗?

java - byte, short, char 是否在 switch 语句中自动提升?

android - Android 中的 DefaultOAuthProvider 出错

android - 使用 AsyncTask 刷新游戏得分 TextView

android - 测试主屏幕小部件的最佳方法

java - Espresso 测试 NestedScrollView - "Error performing ' 滚动到 View 'with id:"

java - java中的身份验证过滤器,帮我消除这个错误

java - 连续同步读写数据通信

java - Android Studio 库不工作

android - 将 Espresso 添加到项目时出现 Mockito 异常