Espresso学习笔记二:Espresso基础知识

一. Espresso基础知识

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);

例如:

onView(withId(R.id.my_view))      // withId(R.id.my_view)是一个ViewMatcher,用来得到指定的视图
  .perform(click())               // click()是一个ViewAction,用在执行一个click动作
  .check(matches(isDisplayed())); // matches(isDisplayed())是一个ViewAssertion,用来验证视图是否已经显示

二. Hamcrest matcher

hamcrest matcher功能非常强大,用于匹配定义的声明。Espresso中的matcher都属于自定义的hamcrest matcher,hamcrest matcher还被用于Mockito、Junit等框架中。例如:onView方法使用一个matcher在视图层中获得唯一一个匹配的视图对象。

Hamcrest本身也包含许多有用的matcher:

  • Core
    • anything - always matches, useful if you don't care what the object under test is
    • describedAs - decorator to adding custom failure description
    • is - decorator to improve readability - see "Sugar", below
  • Logical
    • allOf - matches if all matchers match, short circuits (like Java &&)
    • anyOf - matches if any matchers match, short circuits (like Java ||)
    • not - matches if the wrapped matcher doesn't match and vice versa
    • both, either
  • Object
    • equalTo - test object equality using Object.equals
    • hasToString - test Object.toString
    • instanceOfisCompatibleType - test type
    • any - Is the value an instance of a particular type? Use this version to make generics conform, for example in the JMock clause with(any(Thing.class))
    • notNullValuenullValue - test for null
    • sameInstance - test object identity
    • typeCompatibleWith
    • eventFrom - Tests if the value is an event announced by a specific object
  • Beans
    • hasProperty - test JavaBeans properties
    • getPropertyDescriptor, propertyDescriptorsFor - Utility class for accessing properties on JavaBean objects.
    • samePropertyValuesAs
  • Collections
    • array - test an array's elements against an array of matchers
    • arrayContainingInAnyOrder
    • arrayContaining
    • arrayWithSize, emptyArray
    • hasSize - Matches if collection size satisfies a nested matcher
    • empty - Tests if collection is empty
    • emptyIterable - Tests if collection is empty
    • isIn, isOneOf
    • containsInAnyOrder
    • contains
    • iterableWithSize
    • hasEntryhasKeyhasValue - test a map contains an entry, key or value
    • hasItemhasItems - test a collection contains elements
    • hasItemInArray - test an array contains an element
    • everyItem
  • Number
    • closeTo - test floating point values are close to a given value
    • greaterThangreaterThanOrEqualTolessThanlessThanOrEqualTo - test ordering
    • comparesEqualTo
  • Text
    • equalToIgnoringCase - test string equality ignoring case
    • equalToIgnoringWhiteSpace - test string equality ignoring differences in runs of whitespace
    • containsStringendsWithstartsWith - test string matching
    • isEmptyString, isEmptyOrNullString - Matches empty Strings (and null)
    • stringContainsInOrder
  • Xml
    • hasXPath - Applies a Matcher to a given XML Node in an existing XML Node tree, specified by an XPath expression





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值