Android自動化測試--Espresso使用

上一篇文章:
Android自動化測試--Instrumented Unit Tests使用

相比上一篇文章所講的Instrumented Unit Tests,本文所講的自動化測試Espresso最顯著的特點就是,可以與UI相交互。

使用

首先我們在Android Studio中新建一個項目,取名為EspressoTests。同時刪除自動生成的一些文件,最終目錄結構如下:

目錄結構

接下來我們看看如何一步一步的使用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;
}

接下來我們編寫測試例子,最終的結果如下:

Paste_Image.png

其中MainActivity界面如下,輸入框中點擊數字后點擊計算可以在結果處顯示兩者相加的值,點擊webview跳轉到WebViewActivity,點擊recycleview跳轉到RecycleViewActivity。

MainActivity

其中WebViewActivity中包含一個webview,可以加載跳轉傳過來的url。

WebViewActivity

其中RecycleViewActivity就是一個recycleview的簡單使用,實現了一個列表。

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,看看效果如何

測試結果

測試結果與我們預期的一樣,有一個不匹配。手機運行效果如下:

運行效果.gif

接下來我們就看一看如何在測試中與多個應用界面相交互,歡迎查看下一篇文章:

Android自動化測試--UI Automator使用

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

如果你覺得有用,請在Github不吝給我一個Star,非常感謝。


寫在最后的話:個人能力有限,歡迎大家在下面吐槽。喜歡的話就為我點一個贊吧。也歡迎 Fork Me On Github 。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容