Android EspressoUI自动化
来源:互联网 发布:python kiss原则 编辑:程序博客网 时间:2024/06/05 10:55
Espresso是用来UI自动化测试的框架
使用简单方便
环境搭建
特别简单只需要在build.gradle中添加
dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations'})testCompile 'junit:junit:4.12'
}
栗子:
@RunWith(AndroidJUnit4.class)@SmallTestpublic class InputBikeMessageTest extends BaseViewTest{ Context targetContext ; @Rule public ActivityTestRule<InPutBikeMessageActivity> mActivityRule = new ActivityTestRule( InPutBikeMessageActivity.class,true,true); private AccountModel mAccountModel; @Before public void setUp() throws Exception { targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); mAccountModel = new AccountModel(targetContext); mAccountModel.saveShopToken("1223"); LogUtil.e("------------------执行了setUp @Before"); initMockBikeLockData(); Intent intent = new Intent(targetContext, InPutBikeMessageActivity.class); intent.putExtra("bikeTypeCode", "111"); //启动目标activity mActivityRule.launchActivity(intent); Thread.sleep(500); } /** * gyyx_gxdc-288:当点击锁编号,弹窗显示是否解除锁的绑定操作 */ @Test public void gyyx_gxdc_288() throws InterruptedException { onView(withText("车型1")).perform(click()); //弹出对话框 Thread.sleep(500); //关闭对话框后,item正常显示 onView(withText(targetContext.getString(R.string.bike_producer_dialog_lock_unbinding))).check(ViewAssertions.matches(isDisplayed())); }
}
-----------------------------------------------------------------------------------@SmallTest 测试中不与文件和网络交互
@MediumTest 测试中会与文件交互
@LargeTest 测试中会与文件和网络交互
-------------------------------------------------------------------------------------
@Rule规则
@Rule public ActivityTestRule<InPutBikeMessageActivity> mActivityRule = new ActivityTestRule( InPutBikeMessageActivity.class,true,true);
在这里指定要测试的页面,测试时会跳转InputBikeMessageActivity页面。
测某一个Activity时,只要在这指定一下就ok了,是不是觉得很easy。
------------------------------------------------------------------------------------------
@Before执行测试用例前的初始化
@Test是要执行的测试用例方法。
-------------------------------------------------------------------------------------------
Espresso使用
Espresso主要有三个模块
ViewInteraction onView(Matcher<View>)
通过条件获取View(控件)
onView(withText("车型1")) 获取文本为"车型"的控件
常用:
withText()
withId()
withContentDescription():通过控件的ContentDescription来分辨view,在使用Recycler分辨每项的item时使用到了。
与view的setContentDescription设置的值相对应,当通过id和文字不能分辨时,这是一个选择。
找不到时会报错:NoMatchingViewException
Toast校验
onView(withText("解绑失败")).inRoot(withDecorView(not(is(mActivityRule .getActivity().getWindow().getDecorView())))).check(ViewAssertions.matches(isDisplayed()));
ViewInteraction.perform(ViewAction)
当找到view后执行操作动作。
onView(withText("车型1")).perform(click());点击
常用:
click()点击
doubleClick()双击
typeText();输入文本
clearText()清除文本
swipeLeft/Right/Down/Up 滑动方向
ViewInteraction.check(ViewAssertions)
当找到View后也可以通过check来校验控件的状态
onView(withText("车型2")).check(ViewAssertions.matches(isDisplayed()));车型2 的是否存在
当check时,withText(不存在)不存在时,也不会崩溃。doesNetExist(); 不存在
ViewAssertions.matches(isDisplayed());存在
其它:
1.当Activity需要接受Intent时。
Content targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
Intent intent = new Intent(targetContext, InPutBikeMessageActivity.class);//InPutBikeMessageActivity当前页面intent.putExtra("bikeTypeCode", "111");//启动目标activitymActivityRule.launchActivity(intent);
待续...
- Android EspressoUI自动化
- [android]android自动化测试
- [android]android自动化测试
- [android]android自动化测试
- [android]android自动化测试
- android 自动化测试
- Android自动化测试初探
- Android自动化测试初探
- Android 自动化测试
- Android自动化测试解决方案
- android 自动化测试 monkeyrunner
- Android 手机自动化测试
- Android自动化测试
- android 自动化测试 monkey
- Android 自动化测试初探
- Android自动化测试解决方案
- Android自动化测试初探
- Android自动化测试工具
- JS-多物体运动2(不同物体做不同的运动)
- Date small example
- json:在js中的使用
- EditText右侧小图标点击事件
- 在ARM HI3531 linux上优化FAAC1.28运行速度
- Android EspressoUI自动化
- 【Python】Python 直接赋值、浅拷贝和深度拷贝解析
- SourceTree的使用
- Jquery fileDownload执行成功回调函数不执行的问题
- Retrofit2.0+Rxjava2 相关的文章
- 智能交通是建设智慧城市的第一步!
- JavaWeb文件上传和下载
- Hive的配置项详解
- StringBuffer的字符串操作