UI自动化测试学习笔记:Espresso (一) BasicSample

本Sample演示了键盘输入,然后点击按钮显示在同一个Activity中的TextView以及不同Activity的TextView的UI自动化测试。

界面图

BasicSample

被测试的工程项目

被测试的Sample工程很简单,一个EditView一个TextView,两个Button,键盘输入内容后,点击“CHANGE TEXT”按钮后TextView将 显示输入内容,点击“OPEN ACTIVITY AND CHANGE TEXT”按钮后将打开新的Activity并显示输入内容。

Espresso的工程项目

模拟键盘输入“Espresso”,然后对比输入字符与预期字符是否一致。一致则测试通过,不一致而显示期望值(Expect)和实际值(Got)。
全部测试通过
测试失败

完整的Sample代码

整个测试工程的代码不足100行(包括注释),所以直接贴出代码进行分析

package com.example.android.testing.espresso.BasicSample;

import android.app.Activity;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;


/**
 * Basic tests showcasing simple view matchers and actions like {@link ViewMatchers#withId},
 * {@link ViewActions#click} and {@link ViewActions#typeText}.
 * <p>
 * Note that there is no need to tell Espresso that a view is in a different {@link Activity}.
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ChangeTextBehaviorTest {

    public static final String STRING_TO_BE_TYPED = "Espresso";

    /**
     * A JUnit {@link Rule @Rule} to launch your activity under test. This is a replacement
     * for {@link ActivityInstrumentationTestCase2}.
     * <p>
     * Rules are interceptors which are executed for each test method and will run before
     * any of your setup code in the {@link Before @Before} method.
     * <p>
     * {@link ActivityTestRule} will create and launch of the activity for you and also expose
     * the activity under test. To get a reference to the activity you can use
     * the {@link ActivityTestRule#getActivity()} method.
     */
    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    @Test
    public void changeText_sameActivity() {
        // Type text and then press the button.
        onView(withId(R.id.editTextUserInput))
                .perform(typeText(STRING_TO_BE_TYPED + "\n"), closeSoftKeyboard());
        onView(withId(R.id.changeTextBt)).perform(click());

        // Check that the text was changed.
        onView(withId(R.id.textToBeChanged)).check(matches(withText(STRING_TO_BE_TYPED)));
    }

    @Test
    public void changeText_newActivity() {
        // Type text and then press the button.
        onView(withId(R.id.editTextUserInput)).perform(typeText(STRING_TO_BE_TYPED + "\n"),
                closeSoftKeyboard());
        onView(withId(R.id.activityChangeTextBtn)).perform(click());

        // This view is in a different Activity, no need to tell Espresso.
        onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED)));
    }
}

代码分析

1.类注释中说明,没有必要告诉Espresso被测的view是在不同的Activity中,也就是直接使用 onView()即可获取相应的view
2.@Rule 注解是取代了ActivityInstrumentationTestCase2,是一个拦截器(interceptor),会在@Before的代码运行之前运行。
3. onView()可以根据view的id,hint,text等获取相应的view; perform()属于ViewAction,常见的click,doubleClick等; check()属于ViewInteraction,校验交互后的结果。
4.在changeText_newActivity()方法中,即使跳转了Activity,也直接使用 onView()即可获取相应的view。

运行效果

全部测试通过

注意事项

将输入法换成Android自带键盘,如果使用搜狗、百度等其他中文输入法,则需要将输入的字符串加多一个 \n,不然就会测试失败(仅获取了一个大写字母)
测试失败过

猜你喜欢

转载自blog.csdn.net/scau_zhangpeng/article/details/55003233
今日推荐