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

http://blog.csdn.net/miaoqilong/article/details/46650903

一. Espresso基础知识

Espresso包含4个主要组件:

1. Espresso:和view交互的入口点(通过onView和OnData),还有一些与view无关的APIs(如:pressBack)。

包含以下函数:

[java]  view plain  copy
  1. public static ViewInteraction onView(final Matcher<View> viewMatcher);  
  2. public static DataInteraction onData(Matcher<Object> dataMatcher);  
  3. public static void registerLooperAsIdlingResource(Looper looper);  
  4. public static void registerLooperAsIdlingResource(Looper looper, boolean considerWaitIdle);  
  5. public static void registerIdlingResources(IdlingResource... resources);  
  6. public static void setFailureHandler(FailureHandler failureHandler);  
  7. public static void closeSoftKeyboard();  
  8. public static void openContextualActionModeOverflowMenu();  
  9. public static void pressBack();  
  10. public static void openActionBarOverflowOrOptionsMenu(Context context);  

2. ViewMatchers:一组实现了Matcher<? super View>接口的对象。可以将一个或多个ViewMatchers传入到onView方法中,用来在视图层次中定位视图。

包含以下函数:

[java]  view plain  copy
  1. public static Matcher<View> isAssignableFrom(final Class<?extendsView> clazz);  
  2. public static Matcher<View> withClassName(final Matcher<String> classNameMatcher);  
  3. public static Matcher<View> isDisplayed();  
  4. public static Matcher<View> isCompletelyDisplayed();  
  5. public static Matcher<View> isDisplayingAtLeast(final int areaPercentage);  
  6. public static Matcher<View> isEnabled();  
  7. public static Matcher<View> isFocusable();  
  8. public static Matcher<View> hasFocus();  
  9. public static Matcher<View> hasSibling(final Matcher<View> siblingMatcher);  
  10. public static Matcher<View> withContentDescription(String text);  
  11. public static Matcher<View> withContentDescription(final Matcher<?extendsCharSequence> charSequenceMatcher);  
  12. public static Matcher<View> withId(int id);  
  13. public static Matcher<View> withId(final Matcher<Integer> integerMatcher);  
  14. public static Matcher<View> withTagKey(final int key);  
  15. public static Matcher<View> withTagKey(final int key,final Matcher<Object> objectMatcher);  
  16. public static Matcher<View> withTagValue(final Matcher<Object> tagValueMatcher);  
  17. public static Matcher<View> withText(String text);  
  18. public static Matcher<View> withText(final Matcher<String> stringMatcher);  
  19. public static Matcher<View> withText(final int resourceId);  
  20. public static Matcher<View> isChecked();  
  21. public static Matcher<View> isNotChecked();  
  22. public static Matcher<View> hasContentDescription();  
  23. public static Matcher<View> hasDescendant(final Matcher<View> descendantMatcher);  
  24. public static Matcher<View> isClickable();  
  25. public static Matcher<View> isDescendantOfA(final Matcher<View> ancestorMatcher);  
  26. public static Matcher<View> withEffectiveVisibility(final Visibility visibility);  
  27. public static Matcher<View> withParent(final Matcher<View> parentMatcher);  
  28. public static Matcher<View> withChild(finalMatcher<View> childMatcher);  
  29. public static Matcher<View> isRoot();  
  30. public static Matcher<View> supportsInputMethods();  
  31. public static Matcher<View> hasImeAction(int imeAction);  
  32. public static Matcher<View> hasImeAction(finalMatcher<Integer> imeActionMatcher);  
  33. public static <T>void assertThat(T actual,Matcher<T> matcher);  
  34. public static <T>void assertThat(String message, T actual,Matcher<T> matcher);  

3. ViewActions:一组可以执行的动作(如点击动作:click),可以传入到ViewInteraction.perform方法中。

包含以下函数:

[java]  view plain  copy
  1. public static ViewAction clearText();  
  2. public static ViewAction click();  
  3. public static ViewAction click(ViewAction rollbackAction);  
  4. public static ViewAction swipeLeft();  
  5. public static ViewAction swipeRight();  
  6. public static ViewAction closeSoftKeyboard();  
  7. public static ViewAction pressImeActionButton();  
  8. public static ViewAction pressBack();  
  9. public static ViewAction pressMenuKey();  
  10. public static ViewAction pressKey(int keyCode);  
  11. public static ViewAction pressKey(EspressoKey key);  
  12. public static ViewAction doubleClick();  
  13. public static ViewAction longClick();  
  14. public static ViewAction scrollTo();  
  15. public static ViewAction typeTextIntoFocusedView;  
  16. public static ViewAction typeText(String stringToBeTyped);  

4. ViewAssertions:用来验证视图的状态,需要传到ViewInteraction.check方法中,大多数时候要使用matches函数来验证当前选择的视图的状态。

包含以下函数:

[java]  view plain  copy
  1. public static ViewAssertion doesNotExist();  
  2. public static ViewAssertion matches(final Matcher<? super View> viewMatcher);  
  3. public static ViewAssertion selectedDescendantsMatch(final Matcher<View> selector, final Matcher<View> matcher);  

例如:

[java]  view plain  copy
  1. onView(withId(R.id.my_view))      // withId(R.id.my_view)是一个ViewMatcher,用来得到指定的视图  
  2.   .perform(click())               // click()是一个ViewAction,用在执行一个click动作  
  3.   .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 inthe 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误是因为在你的项目中没有找到 com.android.support.test.espresso:espresso-core:2.2.2 这个依赖库。你需要在你的 build.gradle 文件中添加这个依赖库,或者更新它的版本。具体操作可以参考以下步骤: 1. 打开你的项目中的 build.gradle 文件。 2. 在 dependencies 中添加以下代码: ``` androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' ``` 3. 点击 Sync Now 按钮,等待 Gradle 同步完成。 4. 如果还是出现错误,可以尝试更新依赖库的版本,例如: ``` androidTestImplementation 'com.android.support.test.espresso:espresso-core:3..2' ``` 然后再次点击 Sync Now 按钮,等待 Gradle 同步完成。 希望这些步骤可以帮助你解决这个问题。 ### 回答2: 这是一个涉及到安卓支持库的错误。在开发安卓应用时,我们经常使用安卓支持库,因为这些库提供了许多有用的特性和功能。然而,有时候这些库可能无法正确地导入或编译。导致这种情况的原因可能有很多,我们需要一步步地分析。 首先,我们需要确定是否正确地在build.gradle文件中声明了依赖关系。在这个特定的错误消息中,我们可以看到指示我们所需的库的名称和版本号。因此,我们需要检查我们的build.gradle文件,确保我们正确地声明了依赖关系。我们应该能够找到类似这样的代码: ``` dependencies { ... androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' ... } ``` 如果没有找到这样的代码,我们需要手动添加它。如果找到这样的代码,确保它没有被注释掉或者有任何语法错误。 如果我们已经正确地声明了依赖关系,我们需要检查我们的构建环境。有时候我们的Android Studio环境可能会出现问题,导致无法正确编译库。在这种情况下,我们可以尝试清除Gradle缓存并重新编译。为此,我们可以在Android Studio的菜单中找到“Build” -> “Clean Project”和“Build” -> “Rebuild Project”选项。尝试使用这些选项可能会解决问题。 最后,如果以上方法都没有起作用,可能需要升级或降级所需的库的版本。在这种情况下,我们可以转到Maven仓库网站,查找版本与我们的构建环境和其他库之间的兼容性。我们也可以查看安卓支持库的文档,找到适合我们应用程序的最新版本。 总之,出现这种错误,我们需要逐步检查所有可能出现问题的地方,并使用适当的资源和工具来解决问题。 ### 回答3: 这个错误提示是在使用Android Studio构建应用时出现的。它表明在Gradle构建脚本中引入的Espresso测试框架的核心库无法被解析,即可能没有找到它或者版本不正确。 解决这个问题的方法如下: 1.检查Gradle构建脚本:确保在dependencies部分正确地引入了Espresso测试框架的核心库。例如: dependencies { ... androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' } 2.检查仓库配置:确认项目的仓库配置是否正确,可以在项目根目录的build.gradle文件中添加Google Maven仓库: allprojects { repositories { maven { url 'https://maven.google.com' } jcenter() } } 3.检查版本号:如果上述两个问题都已排除,则可能是版本号不正确。检查Espresso核心库版本是否正确。您可以在以下网址中查找最新版本: https://developer.android.com/training/testing/espresso/setup 4.同步Gradle:如果仍然无法解决问题,则尝试同步Gradle。选择Android Studio菜单中的File > Sync Project with Gradle Files。 总之,要解决这个错误,需要仔细检查Gradle脚本、仓库配置和版本号,并确保正确同步Gradle。这些方法通常可以解决大多数与Espresso测试框架的构建错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值