onView
/**
* Creates a {@link ViewInteraction} for a given view. Note: the view has to be part of the view
* hierarchy. This may not be the case if it is rendered as part of an AdapterView (e.g.
* ListView). If this is the case, use Espresso.onData to load the view first.
*
* <p>This method builds a ViewInteraction object - it does not interact with the application
* under test at all. It is expected that the caller use the ViewInteraction object to perform an
* action or assertion.
*
* @param viewMatcher used to select the view.
* @see #onData(org.hamcrest.Matcher)
*/
// TODO change parameter to type to Matcher<? extends View> which currently causes Dagger issues
@CheckReturnValue
@CheckResult
public static ViewInteraction onView(final Matcher<View> viewMatcher) {
return BASE.plus(new ViewInteractionModule(viewMatcher)).viewInteraction();
}
onView可以通过指定的Matcher来定位到要测试的目标元素,它适用于简单的UI,不适用于AdapterView(指的是ListView,Spinner,GridView等),它有以下限制:
- 要测试的目标元素必须在可见视图内,比如说Listview当前可见item为0-10条,那么就不能用onView来指定11以上(不在屏幕范围内)的元素,不然会报错。
- 它在满足上述条件下可以用来测试Listview,但并不保险。
onData
/**
* Creates an {@link DataInteraction} for a data object displayed by the application. Use this
* method to load (into the view hierarchy) items from AdapterView widgets (e.g. ListView).
*
* <p>This method builds a DataInteraction object - it does not interact with the application
* under test at all. It is expected that the caller use the ViewInteraction object to perform an
* action or assertion.
*
* @param dataMatcher a matcher used to find the data object.
* @return a DataInteraction that will perform an action or assertion.
*/
@CheckReturnValue
@CheckResult
public static DataInteraction onData(Matcher<? extends Object> dataMatcher) {
return new DataInteraction(dataMatcher);
}
onData是通过绑定数据来获取到指定元素,它适用于AdapterView,但不适用于RecyclerView。
- 可以指定屏幕外的元素
- 如果指定屏幕外的元素,它将滑动到指定的元素,然后才会进行后续的操作。
onView使用
@RunWith(AndroidJUnit4.class)
public class EspressoTest {
@Test
public void test(){
onData(withId(R.id.btn)).perform(click());
}
}
onData使用
- 如果List的数据源是String类型
List<String>list;
onData(allOf(instanceOf(String.class),is("item0"))).perform(click());
这里的item0是ListView的第0项的item数据,该数据类型是String类型,allOf将两条匹配规则整合成一条。
- 如果List的数据源是HashMap类型
List<Map<String,String>>list;
onData(hasEntry(is("title"),is("content"))).perform(click());
hasEntry的泛型类型是一个Map类型,如下源码所示:
/**
* Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains
* at least one entry whose key satisfies the specified <code>keyMatcher</code> <b>and</b> whose
* value satisfies the specified <code>valueMatcher</code>.
* <p/>
* For example:
* <pre>assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")))</pre>
*
* @param keyMatcher
* the key matcher that, in combination with the valueMatcher, must be satisfied by at least one entry
* @param valueMatcher
* the value matcher that, in combination with the keyMatcher, must be satisfied by at least one entry
*/
public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K, ? extends V>> hasEntry(org.hamcrest.Matcher<? super K> keyMatcher, org.hamcrest.Matcher<? super V> valueMatcher) {
return org.hamcrest.collection.IsMapContaining.<K,V>hasEntry(keyMatcher, valueMatcher);
}
它的两个参数分别对应HashMap的key和value。
- 如果是Object类型
List<Book>list;
onData(allOf(withBookTitle("item20"),withBookContent("content20"))).perform(click());
public static Matcher<Object> withBookTitle(final String bookTitle){
return new BoundedMatcher<Object, Book>(Book.class) {
@Override
public void describeTo(Description description) {
description.appendText(bookTitle);
}
@Override
protected boolean matchesSafely(Book item) {
return bookTitle.equals(item.getTitle());
}
};
}
public static Matcher<Object> withBookContent(final String bookContent){
return new BoundedMatcher<Object, Book>(Book.class) {
@Override
public void describeTo(Description description) {
description.appendText(bookContent);
}
@Override
protected boolean matchesSafely(Book item) {
return bookContent.equals(item.getContent());
}
};
}
该例子的ListView的每个Item项有两个元素,一个表示title的TextView,一个表示content的TextView,item20表示ListView的第20个item的title。上述Matcher是自定义的Matcher。
网友评论