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 ActivityTestRule mActivityRule = 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);

待续...

更多相关文章

  1. android 自定义控件实现波浪效果(八)
  2. android 自动化压力测试-monkey 3 命令参数
  3. android 自定义控件pickview
  4. 执行Android JUnit测试出现java.net.SocketException: Permissio
  5. 控件布局_FrameLayout(网格布局)
  6. Android控件开发之三----Button
  7. android开源系列:CircleImageView自定义圆形控件的使用
  8. android 调用系统通讯录添加联系人,并将号码传值到相应的控件内
  9. Android之获取控件的坐标

随机推荐

  1. 详细介绍XML在JAVA项目中的作用
  2. 读取XML文件时报“前言中不允许有内容”
  3. 教你如何正确使用XML
  4. 详解JSTL读取xml中文乱码解决的案例
  5. 详细介绍xml的语法的使用和学习
  6. 简单介绍OFBiz使用xml配置界面的想法
  7. xml的属性总结代码详情
  8. XML文件要有根标签(错误)的代码解决分享
  9. 详解xml文档正确格式的示例代码
  10. 具体介绍如何在IE中创建DOM并载入XML的详