如题
如果你是第一次接入和使用Espresso,还没有很熟练的使用语法,那么可以跳过文字,直接继承我下面的代码,先用起来,再回头看看怎么用,挺舒服的。当然,一些注释掉的代码是我也没搞清楚怎么实现的,如果有会的,或者找到可以实现的文章,欢迎评论区留言,谢谢。
代码如下
准备工作
在当前工程的build.gradle里添加如下代码,这样就具备了环境
androidTestImplementation('com.android.support.test:runner:1.0.1'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1'
androidTestCompile ('com.android.support.test.espresso:espresso-intents:2.2.2') {
exclude group: 'com.google.code.findbugs'
}
androidTestCompile 'org.mockito:mockito-android:2.6.3'
我自己的需求
需求是要点击的view是动态的,而且当前屏幕有多个相同名称的view,然后自动打开关闭测试10次。
为啥要先休眠2秒呢,因为此时的view可能还未加载出来,可能会报错,休眠2秒就没问题了。
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* created by lei on 2019/3/28 17:23
*/
@RunWith(AndroidJUnit4.class)
public class MainTabActivityTest extends BaseEspresso{
@Rule
public ActivityTestRule<MainTabActivity> ruleTab = new ActivityTestRule<>(MainTabActivity.class);
@Test
public void clickToRidecodePage() {
sleep(2000);
for (int i = 0; i < 10; i++) {
testClickByDisplayed("出行码");
// onView(allOf(isDisplayed(),firstFindView(withText("出行码")))).perform(closeSoftKeyboard(), click());
sleep(1000);
clickToBack();
sleep(1000);
}
}
}
import android.app.Activity;
import android.support.test.espresso.Espresso;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.clearText;
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.replaceText;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.isDialog;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.core.AllOf.allOf;
/**
* created by lei on 2019/4/1 16:19
*/
public class BaseEspresso<T extends Activity> {
// public IntentsTestRule<T> mActivityRule = new IntentsTestRule<T>((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0], true, true);
// @Rule
// public IntentsTestRule<T> mActivityRule = new IntentsTestRule<T>(GenericsUtils.getSuperClassGenricType(getClass()));
// public T mActivity = null;
/**
* 模拟用户的点击行为
*
* @param id
*/
public void testClick(final int id) {
onView(withId(id)).perform(closeSoftKeyboard(), click());
}
/**
* 模拟用户的点击行为
*
* @param text
*/
public void testClick(String text) {
onView(withText(text)).perform(closeSoftKeyboard(), click());
}
/**
* 点击已显示在屏幕上的view
*
* @param text
*/
public void testClickByDisplayed(String text) {
onView(allOf(isDisplayed(), withText(text))).perform(closeSoftKeyboard(), click());
}
/**
* 点击已显示在屏幕上相同名称的第几个view
* @param text
* @param position
*/
public void testClickByDisplayedWithMatch(String text, int position){
onView(allOf(isDisplayed(), firstFindView(withText(text), position))).perform(closeSoftKeyboard(), click());
}
/**
* 模拟用户的点击行为
*
* @param id
* @param text
*/
public void testClick(final int id, String text) {
onView(allOf(withId(id), withText(text))).perform(closeSoftKeyboard(), click());
}
/**
* 返回
*/
public void clickToBack() {
Espresso.pressBack();
}
public static <T> Matcher<T> firstFindView(final Matcher<T> matcher, int position) {
return new BaseMatcher<T>() {
int index = 0;
@Override
public boolean matches(final Object item) {
if (matcher.matches(item)) {
if (position == index) {
return true;
}
index++;
}
return false;
}
@Override
public void describeTo(final Description description) {
description.appendText("should return first matching item");
}
};
}
/**
* 模拟用户的点击行为
*
* @param id
* @param scrollTo
*/
public void testClick(final int id, boolean scrollTo) {
if (scrollTo) {
onView(allOf(withId(id))).perform(closeSoftKeyboard(), scrollTo(), click());
} else {
onView(allOf(withId(id))).perform(closeSoftKeyboard(), click());
}
}
/**
* 模拟用户的输入文本行为
*
* @param id
* @param text
* @return
*/
public String testInputText(final int id, String text) {
onView(withId(id)).perform(scrollTo(), clearText(), replaceText(text), closeSoftKeyboard());
return text;
}
/**
* 检查View的文本变化是否正确
*
* @param id
* @param text
*/
public void testTextEquals(final int id, String text) {
onView(withId(id)).check(matches(withText(text)));
}
/**
* 检查View是否可见
*
* @param id
*/
public void testViewVisible(final int id) {
onView(withId(id))
.check(matches(isDisplayed()));
}
/**
* 检查View是否可见
*
* @param text
*/
public void testViewVisible(String text) {
onView(withText(text))
.check(matches(isDisplayed()));
}
/**
* 检查View是否可见
*
* @param id
* @param text
*/
public void testViewVisible(final int id, String text) {
onView(allOf(withId(id), withText(text)))
.check(matches(isDisplayed()));
}
/**
* 检查View是否不可见
*
* @param id
*/
public void testViewUnVisible(final int id) {
onView(withId(id))
.check(matches(not(isDisplayed())));
}
/**
* 模拟用户点击Dialog
*
* @param id
*/
public void testDialogClick(int id) {
onView(withId(id)).inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click());
}
/**
* 模拟用户点击Dialog
*
* @param text
*/
public void testDialogClick(String text) {
onView(withText(text)).inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click());
}
/**
* 检查View是否可用
*
* @param id
*/
public void testViewEnable(int id) {
onView(withId(id))
.check(matches(isEnabled()));
}
/**
* 检查View是否不可用
*
* @param id
*/
public void testViewUnEnable(int id) {
onView(withId(id))
.check(matches(not(isEnabled())));
}
/**
* 初始化Activity
*
* @return
*/
// public T getActivity() {
// return getActivity(null);
// }
/**
* 初始化Activity
*
* @return
*/
// public T getActivity(Intent intent) {
// if (intent == null) {
// intent = new Intent();
// }
// if (mActivity == null) {
// mActivityRule.launchActivity(intent);
// mActivity = mActivityRule.getActivity();
// }
// return mActivity;
// }
/**
* 获取可见的Fragment
*
* @return
*/
// public Fragment getVisibleFragment() {
// if (mActivity == null) {
// getActivity();
// }
// if (!(mActivity instanceof FragmentActivity)) {
// return null;
// }
// FragmentManager fm = ((FragmentActivity) mActivity).getSupportFragmentManager();
// if (fm == null || fm.getFragments() == null || fm.getFragments().size() == 0) {
// return null;
// }
// for (int i = fm.getFragments().size() - 1; i >= 0; --i) {
// Fragment fragment = fm.getFragments().get(i);
// if (fragment != null
// && fragment.isResumed()
// && fragment.isVisible()
// && fragment.getUserVisibleHint()) {
// return fragment;
// }
// }
// return null;
// }
/**
* 获取除DialogFragment之外可见的Fragment
*
* @return
*/
// public Fragment getVisibleExcludeDialogFragment() {
// if (mActivity == null) {
// getActivity();
// }
// if (!(mActivity instanceof FragmentActivity)) {
// return null;
// }
// FragmentManager fm = ((FragmentActivity) mActivity).getSupportFragmentManager();
// if (fm == null || fm.getFragments() == null || fm.getFragments().size() == 0) {
// return null;
// }
// for (int i = fm.getFragments().size() - 1; i >= 0; --i) {
// Fragment fragment = fm.getFragments().get(i);
// if (fragment != null
// && !(fragment instanceof DialogFragment)
// && fragment.isResumed()
// && fragment.isVisible()
// && fragment.getUserVisibleHint()) {
// return fragment;
// }
// }
// return null;
// }
/**
* 检查Fragment是否可见
*
* @param type
*/
public void testFragmentVisible(Class type) {
// Assert.assertTrue(type.isInstance(getVisibleFragment()));
}
/**
* 休眠
*
* @param time
*/
public void sleep(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 获取字符串
*
* @return
*/
// public String getString(int resId) {
// if (mActivity == null) {
// getActivity();
// }
// return mActivity.getString(resId);
// }
@Before
public void setUp() {
// 切换RxJava的工作线程
// 在执行测试前,执行用户登录操作,防止对用户信息有依赖
// loginAccount();
}
@After
public void tearDown() {
// 清除用户数据
}
}