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定位具有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)
三、断言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