Android Espresso

2015-05-10 14:00

Espresso 是Android官方提供的一个UI测试框架,适合应用中的功能性 UI 测试。用于测试应用中的用户流,适合用来白盒自动化测试。支持android2.3.4(API 10)及以上版本。

Espresso 是基于instrumentation api,在AndroidJUnitRunner上运行。

配置环境

在module的build.gradle文件中添加依赖

dependencies {
    // Other dependencies ...
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}

使用android studio生成的项目中已经自动添加了改依赖

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
      exclude group: 'com.android.support', module: 'support-annotations'
  })

排斥support-annotations是因为espresso的support-annotations类库与android的support-annotations类库版本冲突

然后关闭模拟器或测试机的动画效果,因为测试用例执行的速度比较快,动画慢的话会影响测试用例的执行(比如消失动画会遮住后面View的点击事件)。
关闭动画步骤:打开设置-开发者选项,关闭以下动画缩放:

窗口动画缩放

过渡动画缩放

动画程序时长缩放

创建测试类

Espresso测试逻辑:

1.通过Espresso.onView(view)来找到要测试的UI组件在Activity中
2.通过ViewInteraction.perform(action)或DataInteraction.perform(action)来模拟用户的操作事件,如文本输入,点击,手势等。可以通过逗号分隔一次传入多个操作事件。
3.如果要测试用例要通过多个activity,重复以上的步骤
4.使用ViewAssertions来检查预期的结果与测试用例结束后的结果是否一致

例如:

onView(withId(R.id.my_view))            // withId(R.id.my_view) is a ViewMatcher
        .perform(click())               // click() is a ViewAction
        .check(matches(isDisplayed())); // matches(isDisplayed()) is a ViewAssertion

登录测试:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginTest{

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
            LoginActivity.class);

    @Before
    public void setup() {
         ...
    }

    @After
    public void tearDown() {
         ...
    }

    @Test
    public void testLogin(){
        onView(withId(R.id.edtName)).perform(typeText("android"), closeSoftKeyboard());
        onView(withId(R.id.edtPwd)).perform(typeText("123456"),closeSoftKeyboard());
        onView(withId(R.id.btnLogin)).perform(click());
        onView(withId(R.id.tvLoginResult)).check(matches(withText("登录成功")));

    }
}

ActivityTestRule 提供单个Activity的方法测试。和JUnit的测试逻辑一致,@Test注解的方法就是会运行的测试方法。@Before即测试方法执行之前会被调用的方法。@After为测试方法执行完成之后会被调用的方法。在所有@Test标注的方法执行时,该Activity会被启动。

要找到对应的View,需要在OnView()方法中传入对应的view matcher。onView方法返回一个ViewInteraction对象,该对象允许测试方法与view交互。但是若onView的对象是RecyclerView,可能不会到达期望的交互结果,需要特殊处理。

若 OnView方法传入的view matcher找不到对应的view.则会抛出NoMatchingViewException,中断测试方法的执行

view matcher

view matcher就是根据一个条件在布局中找到对应的View组件。

如,通过显示的文本内容找到对应的View:

onView(withText("text"));

通过Id:

onView(withId(R.id.edtName))

还可以使用allOf来组合多个条件:

onView(allOf(withId(R.id.button_signin), withText("text")));

allOf可以使用not关键字:

onView(allOf(withId(R.id.button_signin), not(withText("text"))));

为提高测试方法执行的效率,应该尽量使用最少,最快的匹配规则。如通过的唯一的Id能找到对应的View,就不要通过text去匹配。

从AdapterView中定位View

在 AdapterView中,所有子view都是动态生成的在运行中,如果通过onView方法去匹配对应的view,可能会找不到。所以Espresso提供了一个onData方法,该方法会返回一个DataInteraction对象去交互对应的View.Espresso加载目标View到当前的view hierarchy.Espresso同时也处理滚动到目标View,并获取焦点。

例如:

onData(allOf(is(instanceOf(Map.class)),hasEntry(equalTo(LongListActivity.ROW_TEXT), is("test input"))));

执行操作

调用 ViewInteraction.perform() 或 DataInteraction.perform() 方法来模拟用户在View上的交互事件。当传入多个事件时,Espresso会按顺序执行对应的操作。

ViewActions包含多种常用的操作方式:

ViewActions.click() 点击事件

ViewActions.typeText() 点击输入框并输入对应的文本

ViewActions.scrollTo() 滚动到某个View. 对应的View必须是 ScrollView的子类,并且 View的android:visibility属性必须是VISIBLE. 若是 AdapterView 的子类则使需用 onData() 方法来处理

ViewActions.pressKey() 点击按键

ViewActions.clearText() 清除文本框的文本

Espresso-Intents

Espresso Intents 用来验证和模拟应用发出的Intent

添加依赖:

 dependencies {
  // Other dependencies ...
  androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
}

为了测试Intent,你先要创建一个 IntentsTestRule类的实例,和ActivityTestRule作用一致。

@Large
@RunWith(AndroidJUnit4.class)
public class SimpleIntentTest {

    private static final String MESSAGE = "This is a test";
    private static final String PACKAGE_NAME = "com.example.myfirstapp";

    /* Instantiate an IntentsTestRule object. */
    @Rule
    public IntentsTestRule≶MainActivity> mIntentsRule =
      new IntentsTestRule≶>(MainActivity.class);

    @Test
    public void verifyMessageSentToMessageActivity() {

        // Types a message into a EditText element.
        onView(withId(R.id.edit_message))
                .perform(typeText(MESSAGE), closeSoftKeyboard());

        // Clicks a button to send the message to another
        // activity through an explicit intent.
        onView(withId(R.id.send_message)).perform(click());

        // Verifies that the DisplayMessageActivity received an intent
        // with the correct package name and message.
        intended(allOf(
                hasComponent(hasShortClassName(".DisplayMessageActivity")),
                toPackage(PACKAGE_NAME),
                hasExtra(MainActivity.EXTRA_MESSAGE, MESSAGE)));

    }
}

测试WebView

Espresso Web是用来测试WebView的一个组件,它使用 WebDriver API来检查和控制WebView的行为

添加依赖:

dependencies {
  androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'
}

要测试WebView,需要在WebView中允许运行JavaScript。

例:

@LargeTest
@RunWith(AndroidJUnit4.class)
public class WebViewActivityTest {

    private static final String MACCHIATO = "Macchiato";
    private static final String DOPPIO = "Doppio";

    @Rule
    public ActivityTestRule mActivityRule =
        new ActivityTestRule(WebViewActivity.class,
            false /* Initial touch mode */, false /*  launch activity */) {

        @Override
        protected void afterActivityLaunched() {
            // Enable JavaScript.
            onWebView().forceJavascriptEnabled();
        }
    }

    @Test
    public void typeTextInInput_clickButton_SubmitsForm() {
       // Lazily launch the Activity with a custom start Intent per test
       mActivityRule.launchActivity(withWebFormIntent());

       // Selects the WebView in your layout.
       // If you have multiple WebViews you can also use a
       // matcher to select a given WebView, onWebView(withId(R.id.web_view)).
       onWebView()
           // Find the input element by ID
           .withElement(findElement(Locator.ID, "text_input"))
           // Clear previous input
           .perform(clearElement())
           // Enter text into the input element
           .perform(DriverAtoms.webKeys(MACCHIATO))
           // Find the submit button
           .withElement(findElement(Locator.ID, "submitBtn"))
           // Simulate a click via JavaScript
           .perform(webClick())
           // Find the response element by ID
           .withElement(findElement(Locator.ID, "response"))
           // Verify that the response page contains the entered text
           .check(webMatches(getText(), containsString(MACCHIATO)));
    }
}

IdlingResource

Espresso在测试操作中是线程安全的。Espresso会等待当前进程的消息队列中的UI事件,并且在任何一个测试操作中会等待其中的AsyncTask结束才会执行下一个测试。
但是有许多的应用后台的操作都是使用自定义的服务去创建以及管理线程的,这时Espresso就无法同步了。Espresso等待app处于idle状态,才会执行下个动作或检查下个断言。

Idle状态是指应用的当前消息队列中没有UI事件或默认的AsyncTask线程池没有任务。app以其他方式执行长时间异步操作,Espresso无法知道何时这些操作已经完成。所以引入了IdlingResource来告诉Espresso是否可以继续执行测试。

IdlingResource是一个简单的接口,包含三个方法:

getName():必须返回代表idling resource的非空字符串;
isIdleNow():返回当前idlingresource的idle状态。如果返回true,onTransitionToIdle()上注册的ResourceCallback必须必须在之前已经调用;
registerIdleTransitionCallback:通常此方法用于存储对回调的引用来通知idle状态的变化。

例:等待登录接口返回结果

private class LoginIdlingResource implements IdlingResource {

        LoginActivity activity;
        ResourceCallback resourceCallback;

        long startTime = -1;
        long maxTime = 30 * 1000;  //最大等待时间

        public LoginIdlingResource(LoginActivity activity){
           this.activity = activity;
        }

        @Override
        public String getName() {
            return "LoginIdlingResource";
        }


        @Override
        public boolean isIdleNow() {
            Fragment fragment = activity.getFragmentManager().findFragmentByTag(LoadingDialogFragment.TAG);
            long lastTime = startTime == -1 ? maxTime : System.currentTimeMillis() - startTime; //持续时间
            /**
             * 超过最大等待时间
             */
            if(lastTime > maxTime){
                return true;
            }

            if(fragment != null){
                if(startTime == -1){
                    startTime = System.currentTimeMillis();
                }
                resourceCallback.onTransitionToIdle();
                return false;
            }
            return true;
        }

        @Override
        public void registerIdleTransitionCallback(ResourceCallback callback) {
            this.resourceCallback = callback;
        }
    }

调用登录接口时会先弹出LoadingDialogFragment,若LoadingDialogFragment已经能从FragmentManager中找到时说明已经开始调用接口了,调用resourceCallback.onTransitionToIdle(),isIdleNow返回false,告诉Espresso等待。若接口调用完成会关闭LoadingDialogFragment,此时isIdleNow返回true,表示app进入Idle状态,可以继续执行后面的测试。若接口调用失败,LoadingDialogFragment会显示出对应的错误信息,并不会自动关闭,此时还是非Idle状态。所以加入了一个等待时间的最大值,接口等待时间超过这个时长则直接执行后面的检查。

注册IdlingResource

只有注册了的IdlingResource才能被执行,大部分情况下是在@Before方法内注册,在@After内取消注册。


@Before
public void registerIntentServiceIdlingResource() {
    idlingResource = new LoginIdlingResource(mActivityRule.getActivity());
    Espresso.registerIdlingResources(idlingResource);
}

@After
public void unregisterIntentServiceIdlingResource() {
    Espresso.unregisterIdlingResources(idlingResource);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,639评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,093评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,079评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,329评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,343评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,047评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,645评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,565评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,095评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,201评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,338评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,014评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,701评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,194评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,320评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,685评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,345评论 2 358