关于Espresso技术概要

1,实际上提到的view就相当于Element,例如onView(withId(R.id.view))

2,有些时候,一个R.id的值被多个views共享,这时会出现:AmbiguousViewMatcher Exception。这里需要找到view的唯一标识属性,例如:

onView(allOf(withId(R.id.view), withTest("Hello!")))

onView(allOf(withId(R.id.view), not(withTest("unwanted"))))
3,对于一个好的Apps,所有的views对于一个用户来说都应该是包含描述性的text或者有一个内容描述。如果你通过withTest / withContent Description找不到这个view,可以报一个accessibility bug。

4,如果满足条件的话,优先选择R.id

5,Perform操作:perform.(click()),    perform(typeText("Hello"), click()),     perform(scrollTo(), click()),    perform(swipeRight())

6,AdapterView 是一种通过Adapter来动态加载数据的界面元素,常用的有ListView, GridView, Spinner 等都属于AdapterView。不同于我们之前提到的静态的控件,AdapterView 在加载数据时,可能只有一部分显示在屏幕上,对于没有显示的那部分数据,通过onView()是没有办法找到的。于是,Espresso提供了如下方法来查找元素:onData()

onData(allOf(is(instanceOf(String.Class)), is("Americano"))).perform(click())

【注】实际上现在可以通过RecyclerView来替换AdapterView,可以查看:http://blog.csdn.net/seaee/article/details/52094255

7,匹配一个View旁边的另一个View

onView(allOf(withTest("7"), hasSibling(withText("item:0")))).perform(click())
8,JUnit4 Rules,有两种Rules当test with AndroidJUnitRunner,第一种是ActivityTestRule,另一种是ServiceTestRule

ActivityTestRule:提供针对一个Activity 的功能测试,这个Activity will be 启动 before each of @Test 以及 Before any method of @Before. 它将在test完成和@after 方法完成之后结束。

9,匹配Map类型的view, 例如 Map<String, Object>

onData(allOf(is(instanceOf(Map.Class)), hasEntry(equal("STR"), is("item:50")))).perform(click())
10,匹配一个指定view的子view

onData(allOf(is(instanceOf(Map.class)), hasEntry(equal("STR"), is("item:50")))).onChildView(withId(R.id.item.size)).perform(click())
11,匹配一个包含在操作栏(ActionBar)的view

有两种不同的action bars, <1> Normal ActionBar <2> Contextual Action Bar 上下文操作栏

上下文操作栏:所有操作按钮都是出现在ActionBar的位置。

(1)匹配可见的icon:

<1> Normal ActionBar : onView(withId(R.id.xxx))

<2> Contextual Action Bar: onView(withId(R.id.xxx))

(2)匹配overflow menu中的view:

<1> Normal ActionBar:openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext())

<2> Contextual Action Bar: openContextualActionModeOverflowMenu()

12,断言一个view is not displayed

import static org.hamcrest.Matchers.not

onView(withId(R.id.bottom_left)).check(matches(not(isDisplayed())));
13,断言一个view is not present

onView(withId(R.id.bottom_left)).check(doesNotExist());

14,Espresso Web用于自动化测试web元素。

15,通过方法onView()可以获取某个控件的对象,即ViewInteraction。

onView() 有两个方法来获取对象,可以通过withText,例如 ViewInteraction vi_text = onView(ViewMatchers.withText("通过该文本"))。

还可以通过withId,例如 ViewInteraction vi_id = onView(ViewMatchers.withId(R.id.id2))。


Reference: https://google.github.io/android-testing-support-library/docs/espresso/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值