Espresso之 list测试

来源:互联网 发布:无人机软件系统测试 编辑:程序博客网 时间:2024/05/18 01:47

前沿
前面已经介绍了Espresso的一些基础知识,相信看过前面文档,或者在其他地方看过相关文档的同学,已经对Espresso有了一些基本的了解,这篇是对Espress框架的核心部分进行介绍,读懂它,搞定自动化,so easy!


1.AdapterViews like ListView, GridView, Spinner, 和 RecyclerViews

当处理lists(那些以创建RecyclerView或AdapterView),屏幕上并不能显示所有列表中的view,当你scroll()的时候view会被回收,scrollTo() (ViewAction方法 )方法不能在这种情况下使用,因为它需要在一个当前可视的视图中使用。
Espresso提供滚动到具体view或作用于两种特定项目类型的列表的机制:
AdapterViewS和RecyclerView
替换onview()方式,使用ondata(),Espresso将会在适配器中做接下来的匹配工作

2.使用onData()和自定义ViewMatcher匹配数据

下方demo包含一个ListView,它由一个SimpleAdapter适配器提供数据,使用Map

{"STR" : "item: 0", "LEN": 7}

点击具有“item:50”的行的代码如下所示:

onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("STR"), is("item: 50")))  .perform(click());

请注意,Espresso将自动滚动。

让我们拆开Matcher里面onData():

is(instanceOf(Map.class))

将搜索缩小到由Map支持的AdapterView的任何项目。
在我们的例子中,查询的这个方面匹配列表视图的每一行,但是我们想要专门点击一个项目,所以我们进一步缩小搜索:

hasEntry(equalTo("STR"), is("item: 50"))

这Matcher

 return new BoundedMatcher<Object, Map>(Map.class) {    @Override    public boolean matchesSafely(Map map) {      return hasEntry(equalTo("STR"), itemTextMatcher).matches(map);    }    @Override    public void describeTo(Description description) {      description.appendText("with item content: ");      itemTextMatcher.describeTo(description);    }  };}

我们使用BoundedMatcher作为基础,我们覆盖matchesSafely()的方法,传递一个可以匹配到具体元素的Matcher。进行调用withItemContent,进行非空处理

public static Matcher<Object> withItemContent(String expectedText) {  checkNotNull(expectedText);  return withItemContent(equalTo(expectedText));}

现在点击项目的代码很简单:

onData(withItemContent("item: 50")).perform(click());

项目完成代码演示:
https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/sample/src/androidTest/java/android/support/test/testapp/AdapterViewTest.java

3.匹配视图的特定子视图

上面的示例在ListView的整个行的中间发出一个点击。但是,如果我们想要对该行的特定子view操作怎么办?例如,我们想点击的该行的第二列中的特定子view,需要证明做呢:

这里写图片描述

onData(withItemContent("item: 60"))  .onChildView(withId(R.id.item_size))  .perform(click());

注:此示例使用withItemContent()匹配。
示例:

https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/sample/src/androidTest/java/android/support/test/testapp/AdapterViewTest.java

4.RecyclerViewSample:

RecyclerViews与AdapterViews工作方式不同,所以onData()不能用来与他们进行交互。
为了用Espresso 与RecyclerViews交互,espresso-contrib封装具有的集合RecyclerViewActions可用于滚动到的位置或以上的项执行的操作:
scrollTo - 滚动到匹配的视图。
scrollToHolder - 滚动到匹配的查看持有人。
scrollToPosition - 滚动到特定位置。
actionOnHolderItem - 对匹配的查看持有人执行查看操作。
actionOnItem - 对匹配的视图执行查看操作。
actionOnItemAtPosition - 对特定位置的视图执行ViewAction。
从Espresso 的一些例子RecyclerViewSample:
https://github.com/googlesamples/android-testing/tree/master/ui/espresso/RecyclerViewSample

 @Test    public void scrollToItemBelowFold_checkItsText() {        // First scroll to the position that needs to be matched and click on it.        onView(ViewMatchers.withId(R.id.recyclerView))                .perform(RecyclerViewActions.actionOnItemAtPosition(ITEM_BELOW_THE_FOLD, click()));        // Match the text in an item below the fold and check that it's displayed.        String itemElementText = mActivityRule.getActivity().getResources().getString(                R.string.item_element_text) + String.valueOf(ITEM_BELOW_THE_FOLD);        onView(withText(itemElementText)).check(matches(isDisplayed()));    }
  @Test    public void itemInMiddleOfList_hasSpecialText() {        // First, scroll to the view holder using the isInTheMiddle matcher.        onView(ViewMatchers.withId(R.id.recyclerView))                .perform(RecyclerViewActions.scrollToHolder(isInTheMiddle()));        // Check that the item has the special text.        String middleElementText =                mActivityRule.getActivity().getResources().getString(R.string.middle);        onView(withText(middleElementText)).check(matches(isDisplayed()));    }

5总结

1.使用Espresso框架对List 列表进行测试是核心,也是频繁操作的,掌握这篇blog内容是对进行自动化Espresso脚本测试的基础
2.以上内容属于官方文档翻译并增添了一些个人见解。
3.最重要的一点就是需要亲身实践,形成自己的自动化测试体系

1 0
原创粉丝点击