Android EspressoUI自动化
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 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 ActivityTestRulemActivityRule = new ActivityTestRule( InPutBikeMessageActivity.class,true,true);
在这里指定要测试的页面,测试时会跳转InputBikeMessageActivity页面。
测某一个Activity时,只要在这指定一下就ok了,是不是觉得很easy。
------------------------------------------------------------------------------------------
@Before执行测试用例前的初始化
@Test是要执行的测试用例方法。
-------------------------------------------------------------------------------------------
Espresso使用
Espresso主要有三个模块
ViewInteraction onView(Matcher)
通过条件获取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 自定义控件实现波浪效果(八)
- android 自动化压力测试-monkey 3 命令参数
- android 自定义控件pickview
- 执行Android JUnit测试出现java.net.SocketException: Permissio
- 控件布局_FrameLayout(网格布局)
- Android控件开发之三----Button
- android开源系列:CircleImageView自定义圆形控件的使用
- android 调用系统通讯录添加联系人,并将号码传值到相应的控件内
- Android之获取控件的坐标