【Espresso】onData与onView

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.
   *
   * 

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 which currently causes Dagger issues @CheckReturnValue @CheckResult public static ViewInteraction onView(final Matcher 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).
   *
   * 

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 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类型
Listlist;

onData(allOf(instanceOf(String.class),is("item0"))).perform(click());

这里的item0是ListView的第0项的item数据,该数据类型是String类型,allOf将两条匹配规则整合成一条。

  • 如果List的数据源是HashMap类型
List>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 keyMatcher and whose
   * value satisfies the specified valueMatcher.
   * 

* For example: *

assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")))
* * @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 org.hamcrest.Matcher> hasEntry(org.hamcrest.Matcher keyMatcher, org.hamcrest.Matcher valueMatcher) { return org.hamcrest.collection.IsMapContaining.hasEntry(keyMatcher, valueMatcher); }

它的两个参数分别对应HashMap的key和value。

  • 如果是Object类型
Listlist;

onData(allOf(withBookTitle("item20"),withBookContent("content20"))).perform(click());
public static Matcher withBookTitle(final String bookTitle){
        return new BoundedMatcher(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 withBookContent(final String bookContent){
        return new BoundedMatcher(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。

你可能感兴趣的:(【Espresso】onData与onView)