本文介绍了如何检测我在espresso中以编程方式创建的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这行Espresso测试代码:

I have this line of Espresso test code:

    onView(withId(R.id.rvWorkDaySchedule)).perform(swipeDown());

并且rvWorkDaySchedule在Android Studio的编辑器中以红色显示,因为布局中没有这样的XML视图ID-我以编程方式创建了此RecyclerView.

And rvWorkDaySchedule is shown in red in the editor of Android Studio because there is no such XML view id in the layouts - I create this RecyclerView programmatically.

那么我如何检测被Espresso编程放大的视图?

So how do I detect views that have been inflated programmatically with Espresso?

推荐答案

首先,Espresso允许您在测试中使用Hamcrest匹配器.

First of all, Espresso allows you to use Hamcrest matchers in tests.

Hamcrest 1.3快速参考.

捕获以编程方式添加的视图最有用的是withChildwithParenthasSiblinghasDescendant.

The most useful for catching the programmatically added views are withChild, withParent, hasSibling, and hasDescendant.

为了更清楚一点,我将在我的应用程序中举一个简单的示例:

To make it more clear, I would give a simple example from my app:

onView(withId(R.id.action_bar_details))
        .check(matches(withChild(withChild(withText("Details")))));

第二,对于Espresso中的RecyclerView测试,请使用onData方法代替onView.

Secondly, for RecyclerView tests in Espresso use onData methods instead onView.

Espresso 2.1.咖啡速查表大师

我的应用程序中的另一个示例-使用onData方法

Another example from my app - using onData method

onData(anything()).inAdapterView(withId(R.id.listView)).atPosition(getRandomPosition()).
                onChildView(withId(R.id.item)).check(matches(isDisplayed()));

最后,检查这些出色的Google存储库以获取更多示例

Finally, check these great Googles repository for get more examples

  1. GoogleSample
  2. GoogleCodeLabs
  1. GoogleSample
  2. GoogleCodeLabs

这篇关于如何检测我在espresso中以编程方式创建的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 01:09