Android自动化测试--Espresso使用

上一篇文章:
Android自动化测试--Instrumented Unit Tests使用

相比上一篇文章所讲的Instrumented Unit Tests,本文所讲的自动化测试Espresso最显著的特点就是,可以与UI相交互。

使用

首先我们在Android Studio中新建一个项目,取名为EspressoTests。同时删除自动生成的一些文件,最终目录结构如下:

Android自动化测试--Espresso使用_第1张图片
目录结构

接下来我们看看如何一步一步的使用Espresso,首先在�根目录的 build.gradle 文件中添加下面的引入。

ext {
    buildToolsVersion = "24.0.1"
    supportLibVersion = "24.2.0"
    espressoVersion = "2.2.2"
}

在app目录中的build.gradle 文件中添加下面的引入,根据提示点击Sync Now。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // App dependencies
    compile 'com.android.support:appcompat-v7:' + rootProject.supportLibVersion;
    compile 'com.android.support:support-annotations:' + rootProject.supportLibVersion;
    compile 'com.android.support:recyclerview-v7:' + rootProject.supportLibVersion;

    androidTestCompile 'com.android.support.test.espresso:espresso-core:' + rootProject.espressoVersion;
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:' + rootProject.espressoVersion) {
        transitive false
    };
    androidTestCompile 'com.android.support.test.espresso:espresso-web:' + rootProject.espressoVersion;
    androidTestCompile 'com.android.support:support-annotations:' + rootProject.supportLibVersion;
    androidTestCompile 'com.android.support:recyclerview-v7:' + rootProject.supportLibVersion;
}

接下来我们编写测试例子,最终的结果如下:

Android自动化测试--Espresso使用_第2张图片
Paste_Image.png

其中MainActivity界面如下,输入框中点击数字后点击计算可以在结果处显示两者相加的值,点击webview跳转到WebViewActivity,点击recycleview跳转到RecycleViewActivity。

Android自动化测试--Espresso使用_第3张图片
MainActivity

其中WebViewActivity中包含一个webview,可以加载跳转传过来的url。

Android自动化测试--Espresso使用_第4张图片
WebViewActivity

其中RecycleViewActivity就是一个recycleview的简单使用,实现了一个列表。

Android自动化测试--Espresso使用_第5张图片
RecycleViewActivity

接下来我们看看测试的代码,在代码中又详细的注释。

package me.shihao.instrumentedunittests;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.espresso.web.assertion.WebViewAssertions;
import android.support.test.espresso.web.webdriver.DriverAtoms;
import android.support.test.espresso.web.webdriver.Locator;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import me.shihao.espressotests.MainActivity;
import me.shihao.espressotests.R;

import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static   android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.espresso.web.sugar.Web.onWebView;
import static android.support.test.espresso.web.webdriver.DriverAtoms.findElement;
import static android.support.test.espresso.web.webdriver.DriverAtoms.webClick;


/**
 * Created by shihao on 2017/3/2.
 */
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule activityTestRule = new ActivityTestRule(MainActivity.class);

    @Test
    public void test() {
        //通过id找到edittext,在里面输入2并关闭输入法
        Espresso.onView(withId(R.id.editText)).perform(typeText("2"), closeSoftKeyboard());
        //通过id找到edittext,在里面输入5并关闭输入法
        Espresso.onView(withId(R.id.editText2)).perform(typeText("5"), closeSoftKeyboard());
        //通过id找到button,执行点击事件
        Espresso.onView(withId(R.id.button)).perform(click());
        //通过id找到textview,并判断是否与文本匹配
        Espresso.onView(withId(R.id.textView)).check(matches(withText("计算结果:6")));
        Espresso.onView(withId(R.id.textView)).check(matches(withText("计算结果:7")));
    }

    @Test
    public void testRecycleView() {
        //通过文本RecycleView找到按钮,并执行点击事件,跳转到RecycleviewActivity
        Espresso.onView(withText("RecycleView")).perform(click());
        //通过文本"Item 0"找到view,并检查是否显示,然后执行点击事件 ,此时会弹出对话框
        Espresso.onView(withText("Item 0")).check(matches(isDisplayed())).perform(click());
        //通过文本"确定"找到对话框上的确定按钮,执行点击事件,关闭对话框
        Espresso.onView(withText("确定")).perform(click());
        //通过文本"Item 2"找到view,并检查是否显示,然后执行点击事件,此时会弹出对话框
        Espresso.onView(withText("Item 2")).check(matches(isDisplayed())).perform(click());
        //执行点击返回按钮事件,关闭对话框
        Espresso.pressBack();
        //通过id找到recycleview,然后执行滑动事件,滑动到21项
        Espresso.onView(ViewMatchers.withId(R.id.recycleview)).perform(RecyclerViewActions.scrollToPosition(21));
        //通过文本"Item 20"找到view,并检查是否显示,然后执行点击事件,此时会弹出对话框
        Espresso.onView(withText("Item 20")).check(matches(isDisplayed())).perform(click());
        //通过文本"确定"找到对话框上的确定按钮,执行点击事件,关闭对话框
        Espresso.onView(withText("确定")).perform(click());
        //执行点击返回按钮事件,关闭跳转到RecycleviewActivity
        Espresso.pressBack();
    }

    @Test
    public void testWebView() {
        //通过文本RecycleView找到按钮,并执行点击事件,跳转到WebViewActivity
        Espresso.onView(withText("WebView")).perform(click());
        //通过name为"word"找到搜索输入框
        onWebView().withElement(findElement(Locator.NAME, "word"))
                //往输入框中输入字符串"android"
                .perform(DriverAtoms.webKeys("android"))
                //通过id为"index-bn"找到"百度一下"按钮
                .withElement(DriverAtoms.findElement(Locator.ID, "index-bn"))
                //执行点击事件
                .perform(webClick())
                //通过id为"results"找到结果div
                .withElement(DriverAtoms.findElement(Locator.ID, "results"))
                //检查div中是否包含字符串"android"
                .check(WebViewAssertions.webMatches(DriverAtoms.getText(), Matchers.containsString("android")));
        //执行点击返回按钮事件,关闭跳转到WebViewActivity
        Espresso.pressBack();
    }
}

package me.shihao.instrumentedunittests;

import android.content.Intent;
import android.support.test.espresso.web.action.AtomAction;
import android.support.test.espresso.web.assertion.WebViewAssertions;
import android.support.test.espresso.web.sugar.Web;
import android.support.test.espresso.web.webdriver.DriverAtoms;
import android.support.test.espresso.web.webdriver.Locator;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import me.shihao.espressotests.WebViewActivity;

import static android.support.test.espresso.web.sugar.Web.onWebView;
import static android.support.test.espresso.web.webdriver.DriverAtoms.findElement;
import static android.support.test.espresso.web.webdriver.DriverAtoms.webClick;
import static org.hamcrest.Matchers.containsString;

/**
 * Created by shihao on 2017/3/3.
 */
@RunWith(AndroidJUnit4.class)
public class WebViewActivityTest {

    @Rule
    public ActivityTestRule activityTestRule = new ActivityTestRule(WebViewActivity.class, false);

    @Test
    public void test() {
        //传递数据到WebViewActivity
        Intent intent = new Intent();
        intent.putExtra(WebViewActivity.EXTRA_URL, "http://m.baidu.com");
        activityTestRule.launchActivity(intent);

        //通过name为"word"找到搜索输入框
        onWebView().withElement(findElement(Locator.NAME, "word"))
                //往输入框中输入字符串"android"
                .perform(DriverAtoms.webKeys("android"))
                //通过id为"index-bn"找到"百度一下"按钮
                .withElement(DriverAtoms.findElement(Locator.ID, "index-bn"))
                //执行点击事件
                .perform(webClick())
                //通过id为"results"找到结果div
                .withElement(DriverAtoms.findElement(Locator.ID, "results"))
                //检查div中是否包含字符串"android"
                .check(WebViewAssertions.webMatches(DriverAtoms.getText(), Matchers.containsString("android")));

    }
}

接下来我们运行测试一下MainActivityTest,看看效果如何

Android自动化测试--Espresso使用_第6张图片
测试结果

测试结果与我们预期的一样,有一个不匹配。手机运行效果如下:

运行效果.gif

接下来我们就看一看如何在测试中与多个应用界面相交互,欢迎查看下一篇文章:

Android自动化测试--UI Automator使用

Demo代码已经放到了Github上:https://github.com/fodroid/EspressoTests

如果你觉得有用,请在Github不吝给我一个Star,非常感谢。


写在最后的话:个人能力有限,欢迎大家在下面吐槽。喜欢的话就为我点一个赞吧。也欢迎 Fork Me On Github 。

你可能感兴趣的:(Android自动化测试--Espresso使用)