Android-自动化测试Espresso

2018-08-27  本文已影响45人  erki_stwee

参考文章
https://developer.android.com/training/testing/espresso/

最近在看Activity启动过程源码。遇到了Instrumentation这个类,查阅相关资料后发现它尽然可以用来做自动化测试。

Espresso简介

Espresso是Google基于Instrumentation的开源自动化测试框架。核心API小巧,可预测,容易快速上手。Espresso测试清楚的表明期望,交互,检测

Espresso配置

app build.gradle

    compile 'com.android.support:support-annotations:23.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test:runner:0.5'
   
    //异步请求
    compile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2'
    compile 'com.android.support.test.espresso:espresso-contrib:2.2.2'

defaultConfig

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

Espresso的使用步骤

onView(ViewMatcher)
     .perform(ViewAction)
     .check(ViewAssertion);

onData(ObjectMatcher)
     .DataOptions
     .perform(ViewAction)
     .check(ViewAssertion);

案例一

/**
 * 官方案列
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class Rules {

    @Rule
    public ActivityTestRule<MainActivity> mOneActivityRunle =
            new ActivityTestRule<MainActivity>(MainActivity.class);
    
    //测试MainActivity中TextView内容是否一致
    @Test
    public void testText(){
        //检验:MainActivity中是否包含Test文本
        onView(withText("Test")).check(matches(isDisplayed()));
    }

}

案列二

   /**
    * 点击按钮文本内容改变
    */
    @org.junit.Test
    public void clickTest() {
        //检验:一开始,textView不显示
        onView(withId(R.id.text))
                .check(matches(not(isDisplayed())));

        //检验:button文字内容
        onView(withId(R.id.btn))
                .perform(click())
                .check(matches(withText("点击事件")));

        //检验:textView内容是否修改,并且变为可见
        onView(withId(R.id.text))
                .check(matches(withText("点击过后")))
                .check(matches(isDisplayed()));
    }
btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setVisibility(View.VISIBLE);
                text.setText("点击过后");
            }
        });

我们实际开发中绝大多数情况都是异步操作,google爸爸已经替我们想到了这种情况提供了IdlingResource以供开发者进行UI的异步测试

案列三

private android.support.test.espresso.idling.CountingIdlingResource idlingResource;

btnSuccess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    idlingResource.increment();
                    Thread.sleep(5000);
                    text.setVisibility(View.VISIBLE);
                    text.setText("登录成功");
                    editText.setText("");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    idlingResource.decrement();
                }
            }
        });

 public CountingIdlingResource getIdlingResource() {
        return idlingResource;
    }
    @Before
    public void setUp() throws Exception {
        idlingResource = rule.getActivity().getIdlingResource();
    }
    
    @Test
    public void clickSuccess() {

        //注册异步监听,此时测试会被挂起
        //当网络请求结束后,生产代码中Flag状态的改变,会继续执行测试代码
        Espresso.registerIdlingResources(idlingResource);

        //操作:清除edit的内容,然后输入,在关闭键盘,最后校验内容
        onView(withId(R.id.edit))
                .perform(clearText(),
                        replaceText("大胖"),
                        closeSoftKeyboard())
                .check(matches(withText("大胖")));

        //操作:点击Button
        onView(withId(R.id.btn_success))
                .perform(click());

        //操作:textView的内容和可见
        onView(withId(R.id.text))
                .check(matches(withText("登录成功")))
                .check(matches(isDisplayed()));

        onView(withId(R.id.edit))
                .check(matches(withText("")))
                .check(matches(withHint("请输入密码")));
    }

    @After
    public void release() throws Exception {
        Espresso.unregisterIdlingResources(idlingResource);
    }
上一篇下一篇

猜你喜欢

热点阅读