[Espresso 4 Android Doc] 7. Espresso Web

声明:本系列文章是对 Android Testing Support Library官方文档的翻译,水平有限,欢迎批评指正。

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-Web

Espresso-web 是测试 Android 上 WebView 的切入点。它使用流行的 WebDriver API 原子内省并控制 WebView 的行为。

与 onData 类似,WebView 的交互实际上是几个视图原子的组合。一个原子可以被看作一个 ViewAction,一个在 UI 上执行操作的自包含单元。然而,它们需要适当的精心策划并且十分啰嗦。Web 和 WebInteraction 对此样本进行了包装,提供了 Espresso 风格的 WebView 交互体验。

WebView 经常在 Java/JavaScript 之间跨界工作,由于没有机会将 JavaScript 中的数据引入到竞态机制(Espresso 得到的所有 Java 端的数据都一个独立的副本),WebInteractions 全面支持数据的返回。

常规 WebInteractions

  • ​withElement(ElementReference)​ 将把 ​ElementReference​ 提交到原子中,示例如下:
onWebView().withElement(findElement(Locator.ID, "teacher"))
  • withContextualElement(Atom<ElementReference>) 将把 ElementReference 提交到原子钟,示例如下:
onWebView()
  .withElement(findElement(Locator.ID, "teacher"))
  .withContextualElement(findElement(Locator.ID, "person_name"))
  • ​check(WebAssertion)​ 将会检查断言的真假性。示例如下:
onWebView()
  .withElement(findElement(Locator.ID, "teacher"))
  .withContextualElement(findElement(Locator.ID, "person_name"))
  .check(webMatches(getText(), containsString("Socrates")));
  • ​perform(Atom)​ 将在当前的上下文中执行提供的原子操作,示例如下:
onWebView()
  .withElement(findElement(Locator.ID, "teacher"))
  .perform(webClick());
  • 当之前的操作(如点击)改变了界面导航,从而使 ElementReference 和 WindowReference 点失效时,必须使用 ​reset()​。

WebView 示例

Espresso web 需要启用 JavaScript 来控制 WebView。你可以通过覆写 ActivityTestRule 类中的 afterActivityLaunched 方法强制启用它。

@Rule
public ActivityTestRule<WebViewActivity> mActivityRule = new ActivityTestRule<WebViewActivity>(WebViewActivity.class, false, false) {
    @Override
    protected void afterActivityLaunched() {
        // Enable JS!
        onWebView().forceJavascriptEnabled();
    }
}

@Test
public void typeTextInInput_clickButton_SubmitsForm() {
   // Lazily launch the Activity with a custom start Intent per test
   mActivityRule.launchActivity(withWebFormIntent());

   // Selects the WebView in your layout. If you have multiple WebViews you can also use a
   // matcher to select a given WebView, onWebView(withId(R.id.web_view)).
   onWebView()
       // Find the input element by ID
       .withElement(findElement(Locator.ID, "text_input"))
       // Clear previous input
       .perform(clearElement())
       // Enter text into the input element
       .perform(DriverAtoms.webKeys(MACCHIATO))
       // Find the submit button
       .withElement(findElement(Locator.ID, "submitBtn"))
       // Simulate a click via javascript
       .perform(webClick())
       // Find the response element by ID
       .withElement(findElement(Locator.ID, "response"))
       // Verify that the response page contains the entered text
       .check(webMatches(getText(), containsString(MACCHIATO)));
}

在 GitHub 上查看 Espresso Web sample

下载 Espresso-Web

  • 确保你已经安装了 Android Support Repository(查看说明
  • 打开应用的 ​build.gradle​ 文件。它通常不是顶级 ​build.gradle​,而是​​app/build.gradle​。

在 dependencies 中添加以下行:

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

Espresso-Web 只兼容 Espresso 2.2+ 和 testing supprot 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'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,975评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,489评论 2 45
  • 离开你很久了像茶喝过很多遍后渐渐淡下来我以为感情也如此 岁月无声地流逝突然你又出现在我面前字里行间仿佛又回到了过去...
    一生如燕阅读 291评论 0 4
  • 依然直接上代码,很简单 2行 就实现第一种解决方案: 第二种解决方案: 提示当设置后,文字依然跑偏,解决方案:首先...
    1b3bd36d9d21阅读 15,398评论 6 5
  • 病毒 北上的火车轰隆隆的前行,身后的红土地在热带阔叶林的掩映下,渐渐消失,四年前也...
    穴鸟阅读 459评论 0 0