1. Espresso 概览
2. Espresso 设置说明
3. Espresso 基础
4. Espresso 备忘录
5. Espresso 意图
6. Espresso 高级示例
7. Espresso Web
8. AndroidJUnitRunner
9. ATSL 中的 JUnit4 规则
10. UI Automator
11. 可访问性检查

Espresso-Intents 是 Espresso 的一个扩展,它使验证和存根待测应用向外发出的意图成为可能。它类似于 Mockito,但是针对的是 Android 的意图。

下载 Espresso-Intents

  • 确保你已经安装了 Android Support Repository(参考 instructions)。
  • 打开你应用的 ​build.gradle​ 文件。这一般不是顶级的 ​build.gralde​ 文件,而是 ​app/build.gradle​。

在 dependencies 中添加以下行:

androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'

Espresso-Intents 只兼容 Espresso 2.1+ 和 testing support library 0.3。所以确保你也更新了以下行:

androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

IntentsTestRule

当使用 Espresso-Intents 时,应当用 ​IntentsTestRule​ 替换 ​ActivityTestRule​。IntentsTestRule​ 使得在 UI 功能测试中使用 Espresso-Intents API 变得简单。该类是 ​ActivityTestRule​ 的扩展,它会在每一个被 ​@Test​ 注解的测试执行前初始化 Espresso-Intents,然后在测试执行完后释放 Espresso-Intents。被启动的 activity 会在每个测试执行完后被终止掉,此规则也适用于 ​ActivityTestRule​。

验证意图(Intent validation)

Espresso-Intents 会记录待测应用里所有尝试启动 Activity 意图。使用 intended API(与 ​Mockito.verify​ 类似)你可以断言特定的意图是否被发出。

验证外发意图的简单示例:

@Test
public void validateIntentSentToPackage() {// User action that results in an external "phone" activity being launched.user.clickOnView(system.getView(R.id.callButton));// Using a canned RecordedIntentMatcher to validate that an intent resolving// to the "phone" activity has been sent.intended(toPackage("com.android.phone"));
}

意图存根(Intent stubbing)

使用 intending API(与 ​Mockito.when​ 类似)你可以为通过 startActivityForResult 启动的 Activity 提供一个响应结果(尤其是外部的 Activity,因为我们不能操作外部 activity 的用户界面,也不能控制 ​ActivityResult​ 返回给待测 Activity)。

使用意图存根的示例:

@Test
public void activityResult_IsHandledProperly() {// Build a result to return when a particular activity is launched.Intent resultData = new Intent();String phoneNumber = "123-345-6789";resultData.putExtra("phone", phoneNumber);ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);// Set up result stubbing when an intent sent to "contacts" is seen.intending(toPackage("com.android.contacts")).respondWith(result));// User action that results in "contacts" activity being launched.// Launching activity expects phoneNumber to be returned and displays it on the screen.onView(withId(R.id.pickButton)).perform(click());// Assert that data we set up above is shown.onView(withId(R.id.phoneNumber).check(matches(withText(phoneNumber)));
}

意图匹配器(Intent Matchers)

​intending​ 和 ​intended​ 方法用一个 hamcrest ​Matcher<Intent>​ 作为参数。 Hamcrest 是匹配器对象(也称为约束或断言)库。有以下选项:

  • 使用现有的意图匹配器:最简单的选择,绝大多数情况的首选。
  • 自己实现意图匹配器,最灵活的选择(参考 Hamcrest 教程 的 “Writing custom matchers” 章节)

以下是一个使用现有的意图匹配器验证意图的示例:

intended(allOf(hasAction(equalTo(Intent.ACTION_VIEW)),hasCategories(hasItem(equalTo(Intent.CATEGORY_BROWSABLE))),hasData(hasHost(equalTo("www.google.com"))),hasExtras(allOf(hasEntry(equalTo("key1"), equalTo("value1")),hasEntry(equalTo("key2"), equalTo("value2")))),toPackage("com.android.browser")));
 Android Testing

(8.1.5.5)Android Testing Support Library翻译之Espresso 意图相关推荐

  1. (8.1.5.4)Android Testing Support Library翻译之Espresso 备忘录

    1. Espresso 概览 2. Espresso 设置说明 3. Espresso 基础 4. Espresso 备忘录 5. Espresso 意图 6. Espresso 高级示例 7. Es ...

  2. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.2——使用Android Testing Support Library进行测试...

    问题: 你想要测试app的Android组件. 解决方案: 使用新的测试类实现JUnit风格的测试. 讨论: 测试像activities,services等的Android组件,需要将app部署到连接 ...

  3. Android Design Support Library初探-更新中

    导读 这个兼容库容易和Google之前发布的 Android Support Library 22.1 混淆,两者的区别在于: Android Support Library 22.1 只是支持了一些 ...

  4. Android Design Support Library 中控件的使用简单介绍(一)

    Android Design Support Library 中控件的使用简单介绍(一) 介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android ...

  5. Material Design 开发利器:Android Design Support Library 介绍

    Material Design 开发利器:Android Design Support Library 介绍 本文翻译自著名博客 Android Developers,原作者是 Ian Lake,点击 ...

  6. Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    目前,我相信,没有任何 Android 开发者不知道材料设计的,因为它的设计在过去的一年震惊了世界,正式的变成了一个设计理念. 令人惊讶的是,在 Android 应用中材料设计是不容易实现的,因为材料 ...

  7. 如何更新android v7 support library,新手入门之Android Support Library | Soo Smart!

    Support Library 这个支持包是一个一系列代码库,它提供了Android framework APIs的向后兼容的版本以及一些只有通过这个库包API才有的feature特性.每个Suppo ...

  8. android support design library eclipse,Eclipse下使用Android Design Support Library中的控件

    我只测试了 Design效果的Login和Scrolling效果控件. 最下面奉上2个demo. 1.File\import导入sdk\extras\android\support\V7\appcom ...

  9. Android 各个Support Library介绍

    参考: http://zmywly8866.github.io/2015/10/09/android-support-library.html V4 Support Library 这个包的名字是:& ...

最新文章

  1. DOMContentLoaded 与onload区别以及使用
  2. 从volatile解读ConcurrentHashMap(jdk1.6.0)无锁读
  3. 图像处理------应用卷积一实现噪声消去
  4. [恢]hdu 1865
  5. SP和Fuction的关系
  6. leetcode1219. 黄金矿工(回溯)
  7. PJSIP学习笔记——从simple_pjsua.c示例程序了解PJSUA-LIB的基本使用流程
  8. mongoddb常用增删改查命令--推荐查询命令:
  9. java中md5校验工具,java实现MD5文件校验
  10. 思科路由器2811如何重设密码
  11. 关于在线银行卡支付限额问题 解决方案
  12. 案例分享:Qt工程机械真空激光焊接系统软件产品定制(西门子PLC,mysql数据库,用户权限控制,界面配置,参数定制,播放器,二维图标,rgv小车,期限控制,参数调试等)
  13. 2023年AI十大展望:GPT-4领衔大模型变革,谷歌拉响警报,训练数据告急
  14. 实验一 MiniOS
  15. 万能码码上付给你一个惊喜(安全扫码专业委员会)
  16. android studio release包安装后打不开,debug包安装正常怎么解决
  17. C++实现Photoshop图层颜色混合模式
  18. Grafana 的介绍和安装
  19. 步进电机相数、细分、步距角、接线方法
  20. 以太坊的企业系统集成

热门文章

  1. [凯立德]2013.12.17凯立德发布秋季版(2F21J0E)最新增量包SP1_我是亲民_新浪博客...
  2. 如何用R做计量经济学
  3. tomcat启动一闪而过的解决方案
  4. IT人如何开始自己创业(轉)
  5. res\values-sw360dp\dimens.xml.orig:518: error:
  6. matlab中常用函数的应用
  7. caused by java.io.io_疑难杂症之 Error:java.io.InvalidClassException
  8. 2009年2月28号发现,上海移动升级GPRS时出问题,导致一些用户话费被扣钱,甚至停机...
  9. vue开发项目必备知识
  10. Cisco ASA防火墙