Espress
谷歌13年的时候开源了espress,谷歌的思路是,等到它足够成熟和稳定以后,将其迁移到Android SDK中,以此可见对他的重视。Google使用Espresso测试了他们自己的超过30个应用程序,包括G+、Maps和Drive。
它由三部分组成:
ViewMachers:寻找用来测试的View。
ViewActions:发送交互事件。
ViewAssertions:检验测试结果。
常用api
assertture(“校验的文本”,true)
//EditText文本输入
TypeText(String) - 一个个字输入
EnterText(String) - 一串数据输入
//等待页面中的文本
waitfortext(string,多少个相匹配,等待时间(毫秒))
waitfortext(id,多少个相匹配,等待时间(毫秒))
waitForActivity()
//查看页面中的文本
SearchText()
SearchButton()
SearchEditText()
waitfortext、searchtext可以获取toast message
//点击按钮
ClickOnText(String)
ClickOnButton(String)
ClickOnCheckBox
ClickOnRadioButton
ClickInList(第几行)
ClickOnView(GetView(id)) - 点击页面中难找到的控件
ClickOnImage(index) - 点击页面中图片按钮,如账号中的“X”按钮
//清除控件中的文本
ClearEditText
//滚动
ScroolDown - 向下滚动屏幕
scrollListtoline(index,多少行) - 向下滚动列表框多少行
scrolltoside - 水平滚动屏幕
//屏幕控制
setActivityOrientation(Solo.LANDSCAPE或Solo.PORTRAIT) 控制屏幕横向或纵向显示
//跳转
goBack() 模仿硬返回键
goBackToActivity(“Activity名”) 跳到指定的Activity
//截屏
takescreenshot
Espresso包含4个主要组件:
1. Espresso:和view交互的入口点(通过onView和OnData),还有一些与view无关的APIs(如:pressBack)。
包含以下函数:
public static ViewInteraction onView(final Matcher<View> viewMatcher);
public static DataInteraction onData(Matcher<Object> dataMatcher);
public static void registerLooperAsIdlingResource(Looper looper);
public static void registerLooperAsIdlingResource(Looper looper, boolean considerWaitIdle);
public static void registerIdlingResources(IdlingResource... resources);
public static void setFailureHandler(FailureHandler failureHandler);
public static void closeSoftKeyboard();
public static void openContextualActionModeOverflowMenu();
public static void pressBack();
public static void openActionBarOverflowOrOptionsMenu(Context context);
2. ViewMatchers:一组实现了Matcher<? super View>接口的对象。可以将一个或多个ViewMatchers传入到onView方法中,用来在视图层次中定位视图。
包含以下函数:
public static Matcher<View> isAssignableFrom(final Class<?extendsView> clazz);
public static Matcher<View> withClassName(final Matcher<String> classNameMatcher);
public static Matcher<View> isDisplayed();
public static Matcher<View> isCompletelyDisplayed();
public static Matcher<View> isDisplayingAtLeast(final int areaPercentage);
public static Matcher<View> isEnabled();
public static Matcher<View> isFocusable();
public static Matcher<View> hasFocus();
public static Matcher<View> hasSibling(final Matcher<View> siblingMatcher);
public static Matcher<View> withContentDescription(String text);
public static Matcher<View> withContentDescription(final Matcher<?extendsCharSequence> charSequenceMatcher);
public static Matcher<View> withId(int id);
public static Matcher<View> withId(final Matcher<Integer> integerMatcher);
public static Matcher<View> withTagKey(final int key);
public static Matcher<View> withTagKey(final int key,final Matcher<Object> objectMatcher);
public static Matcher<View> withTagValue(final Matcher<Object> tagValueMatcher);
public static Matcher<View> withText(String text);
public static Matcher<View> withText(final Matcher<String> stringMatcher);
public static Matcher<View> withText(final int resourceId);
public static Matcher<View> isChecked();
public static Matcher<View> isNotChecked();
public static Matcher<View> hasContentDescription();
public static Matcher<View> hasDescendant(final Matcher<View> descendantMatcher);
public static Matcher<View> isClickable();
public static Matcher<View> isDescendantOfA(final Matcher<View> ancestorMatcher);
public static Matcher<View> withEffectiveVisibility(final Visibility visibility);
public static Matcher<View> withParent(final Matcher<View> parentMatcher);
public static Matcher<View> withChild(finalMatcher<View> childMatcher);
public static Matcher<View> isRoot();
public static Matcher<View> supportsInputMethods();
public static Matcher<View> hasImeAction(int imeAction);
public static Matcher<View> hasImeAction(finalMatcher<Integer> imeActionMatcher);
public static <T>void assertThat(T actual,Matcher<T> matcher);
public static <T>void assertThat(String message, T actual,Matcher<T> matcher);
3. ViewActions:一组可以执行的动作(如点击动作:click),可以传入到ViewInteraction.perform方法中。
包含以下函数:
public static ViewAction clearText();
public static ViewAction click();
public static ViewAction click(ViewAction rollbackAction);
public static ViewAction swipeLeft();
public static ViewAction swipeRight();
public static ViewAction closeSoftKeyboard();
public static ViewAction pressImeActionButton();
public static ViewAction pressBack();
public static ViewAction pressMenuKey();
public static ViewAction pressKey(int keyCode);
public static ViewAction pressKey(EspressoKey key);
public static ViewAction doubleClick();
public static ViewAction longClick();
public static ViewAction scrollTo();
public static ViewAction typeTextIntoFocusedView;
public static ViewAction typeText(String stringToBeTyped);
4. ViewAssertions:用来验证视图的状态,需要传到ViewInteraction.check方法中,大多数时候要使用matches函数来验证当前选择的视图的状态。
包含以下函数:
public static ViewAssertion doesNotExist();
public static ViewAssertion matches(final Matcher<? super View> viewMatcher);
public static ViewAssertion selectedDescendantsMatch(final Matcher<View> selector, final Matcher<View> matcher);
备忘录: