码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-03-17 20:59:40      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:pack   void   str   cli   private   pre   expec   view   support   

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测试

标签:pack   void   str   cli   private   pre   expec   view   support   

原文地址:http://www.cnblogs.com/pujun/p/6568153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!