Espresso框架使用教程

Espresso框架

1. 前期准备

1.1 依赖配置

1.2 排除干扰项

2. Espresso架构

2.1 定位View(onView/onData)

2.2 [操作View(perform)】(#操作View)

2.3 [断言View(check)】(#断言View)

前期准备

依赖配置

1、添加依赖

androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1')
androidTestImplementation 'com.android.support.test:runner:1.0.2'
implementation files('libs/mockito-core-2.21.0.jar')
testImplementation 'junit:junit:4.12'

2、配置测试运行器

在app模块下build.gradle文件内defaultConfig区域配置testIntrumentRunner

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

排除干扰项

开发者选项下关闭一些可能影响的配置 过渡动画缩放 窗口动画缩放 动画程序时长缩放

Espresso架构

 Espresso有三个重要的类,分别是Matchers(匹配器),ViewAction(界面行为),ViewAssertions(界面判断),其中Matchers是常常是通过匹配条件来需找UI组件或过滤UI,而ViewAction是来模拟用户操作界面的行为,ViewAssertions对模拟行为操作的View进行变换和结果验证

一、定位View(onView/onData)

定位元素的类为com.android.support.test.espresso.Espresso 该类下有两个方法onView()与onData() 定位普通View 方法名:com.android.support.test.espresso.Espresso.onVew() 方法声明 public static ViewInteraction onView(final Matcher viewMatcher) 入参 ViewMatchers 用于表述要寻找的View 工具类 android.support.test.espresso.matcher.ViewMatchers ViewMatchers类中有多种静态工厂方法用于生成定位元素的ViewMatcher(见普通View定位原理部分) 返回值 ViewInteraction 代表一个可被操作的View      android.support.test.espresso.ViewInteraction 定位原理 id ViewMatchers.withId() text ViewMatchers.withText() 类类型 ViewMatchers.instanceOf() 当前拥有焦点 ViewMatchers.hasFocus() 父布局 。。。

定位具有adapter的View onData()
方法名 com.android.support.test.espresso.Espresso.onData()
方法声明 public static DataInteraction onData(Matcher<? extends Object> dataMatcher)
定位原理 定位继承adapter的View 例如定位ListView中的某一行
传入参数 DataMatchers 用于表述要寻找的继承Adapter的View
     类 ViewMatchers.instanceOf
     。。。 
返回值 DataInteraction 代表着可被操作的数据集 android.support.test.espresso.DataInteraction

二、操作View(perform)

元素上执行操作(perform) ViewInteraction/DataInteraction perform() 可链式调用 viewInteraction.perform(xx1).perform(xx2)...; 参数 ViewActions工具类下 android.support.test.espresso.action.ViewActions 常用操作    click():点击操作    typeText():输入文字    clearText():清除文字    replaceText():替换文字 。。。

三、断言View(check)

三、判断执行结果check
check()方法 android.support.test.espresso.ViewInteraction.check()
传入参数 ViewAssertion ViewAssertions工具类

四、线程同步

四、线程同步
IddlingResoureces
https://developer.android.google.cn/reference/android/support/test/uiautomator/UiWatcher

参考文献
https://www.jianshu.com/p/c5da84eae63b
https://www.jianshu.com/p/474f276986ac
https://blog.csdn.net/u013247461/article/details/81042763

posted @ 2020-04-15 00:24  天下布喵  阅读(637)  评论(0)    收藏  举报