java - 如何使用 Espresso 框架单击上下文操作栏项目

标签 java android android-actionbar android-espresso contextual-action-bar

我正在编写一个测试用例,长按我的 ListView 中的一个项目(由 CursorAdapter 支持),这将拉出一个上下文操作栏并有一个允许用户删除该项目的菜单项。

我使用以下代码:

public void testDeleteFirstAccount(){
    // Long click
    onData(is(instanceOf(Cursor.class))).atPosition(0).perform(longClick());

    // Click delete menu item
    onView(withId(R.id.action_delete_account)).perform(click());

    // Find alert button with text
    onView(withText("Yes")).perform(click());
}

但是,我无法单击该按钮,因为我认为测试运行得太快了。我收到以下错误:

android.support.test.espresso.PerformException: Error performing 'single click' on view 'with id: com.example.android.cashcaretaker:id/action_delete_account'.

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.

我认为测试速度太快的原因是,如果我在长按后添加 Thread.sleep(),一切都会正常进行。我这样做只是为了验证我的怀疑,我不确定像那样停止 UI 是继续前进的正确方法。

我也尝试添加 getInstrumentation().waitForIdleSync() 但没有成功。

我做错了什么吗?使用 Espresso 单击 CAB 项目的正确方法是什么?

最佳答案

也许在 Espresso 尝试单击 ContextualActionBar 项目时, View 仍然处于动画状态。如果您设置超时,则动画有时间完成,并且在执行单击时 View 已完全显示。

您是否尝试过按照 Espresso Setup guide 中所述在测试设备中禁用动画?

关于java - 如何使用 Espresso 框架单击上下文操作栏项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29909992/

相关文章:

java - 如何将数组作为节点列表返回

ActionBar/Tabs 中的 Android 自定义主题

java - Groovy StreamingTemplateEngine IOException : mark() not supported

java - 两个骰子 throw 游戏问题

android - 如何在删除 Realm 文件之前在 Android 中备份 Realm DB。有什么办法可以恢复备份文件吗?

Android:IntentService 启动但 onHandleIntent 未执行

android - 在 xml 中更改 Gingerbread 上的操作栏 Logo

android - 使用工具栏中的 SearchView 自动打开/关闭键盘

java - 使用java ConcurrentSkipListSet add方法时线程卡住

java - 如何删除FTP目录中的所有txt文件?