在Android Studio环境下使用ESPRESSO 测试框架进行UI测试

Posted 微笑扯痛回忆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Android Studio环境下使用ESPRESSO 测试框架进行UI测试相关的知识,希望对你有一定的参考价值。

1.首先,在后缀为androidTest的文件夹内建立一个MainActivityInstrumentedTest的Java文件,

package com.example.pj.git;

import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;

import org.hamcrest.Matcher;
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;

/**
* Created by pj on 2017/3/15.
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityInstrumentationTest {
private static final String STRING_TO_BE_TYPED = "Peter";

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

@Test
public void sayHello() {
onView(withId(R.id.et)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
onView(withText("Say hello!")).perform(click());
String expectedText = "Hello," + STRING_TO_BE_TYPED + "!";
onView(withId(R.id.tv)).check(matches(withText(expectedText)));
}
}

2.然后在模拟器上运行MainActivityInstrumentedTest,如果运行成功会提示

如果运行失败会提示

 


以上是关于在Android Studio环境下使用ESPRESSO 测试框架进行UI测试的主要内容,如果未能解决你的问题,请参考以下文章

在ANDROID STUDIO环境下使用ESPRESSO 测试框架进行UI测试

在ANDROID STUDIO环境下使用Espresso 测试框架进行UI测试

Android Studio下gradle环境搭建

Mac下Android studio搭建Android开发环境新手

Android 开发之Windows环境下Android Studio安装和使用教程(图文详细步骤)

Ubuntu环境下配置Android Studio