Espresso Web

Espresso-Web 是用于与 Android WebView 界面组件进行交互的入口点。Espresso-Web 重复使用常用 WebDriver API 中的 Atom 来检查和控制 WebView 的行为。

何时使用 Espresso-Web

使用 Espresso-Web 测试混合应用,尤其是应用的原生界面组件与其 WebView 界面组件的集成。您可以将 Espresso-Web API 与其他 Espresso API 结合使用,以便与 WebView 对象内的 Web 元素进行全面交互。

如果您只需要测试 WebView 本身,而不需要测试 WebView 与应用中的原生组件之间的互动,请考虑使用 WebDriver 等框架编写常规 Web 测试。如果您使用 Web 测试框架,则无需使用 Android 设备或 Java 虚拟机,这可让您的测试运行得更快、更可靠。话虽如此,Espresso-Web 允许您重复使用自定义 WebDriver Atom,这为您提供了很大的灵活性,尤其是在编写计划针对独立 Web 应用和包含 Android 界面的应用运行的测试时。

运作方式

与 Espresso 的 onData() 方法类似,一次 WebView 互动由多个 Atom 组成。WebView 互动是通过结合 Java 编程语言与 JavaScript 桥接机制来实现的。由于通过公开 JavaScript 环境中的数据来引入竞态条件的可能性为零(Espresso 在基于 Java 的一侧看到的所有内容都是隔离的副本),因此完全支持从 Web.WebInteraction 对象返回数据,从而允许您验证从请求返回的所有数据。

什么是 WebDriver Atom?

WebDriver 框架通过 Atom 以编程方式查找和操作网页元素。Atom 由 WebDriver 用来进行浏览器操作。Atom 在概念上类似于 ViewAction,它是在界面中执行操作的独立单元。您可以使用 findElement() 和 getElement() 等已定义方法的列表来提供 Atom,以从用户的角度驱动浏览器。不过,如果您直接使用 WebDriver 框架,Atom 需要经过适当编排,这需要非常详细的逻辑。

在 Espresso 中,Web 和 Web.WebInteraction 类(位于 androidx.test.espresso.web.sugar 软件包中)对此样板代码进行封装,使得与 WebView 的互动也能保持类似 Espresso 的体验。因此,在 WebView 的上下文中,Atom 用来替代传统的 Espresso ViewMatchers 和 ViewActions(如需了解标准匹配器和操作,请参阅 Espresso 备忘单)。

于是,API 看起来非常简单:

Kotlin

onWebView()
    .withElement(Atom)
    .perform(Atom)
    .check(WebAssertion)

Java

onWebView()
    .withElement(Atom)
    .perform(Atom)
    .check(WebAssertion);

要了解详情,请阅读 Selenium 的 Atom 文档。

实现 WebView

请按照以下部分中显示的指南在应用的测试中使用 WebView。

软件包

如需在项目中添加 Espresso-Web,请完成以下步骤:

  1. 打开应用的 build.gradle 文件。这通常不是顶级 build.gradle 文件,而是 app/build.gradle。
  2. 在依赖项内添加以下代码行:

    Groovy

        androidTestImplementation 'androidx.test.espresso:espresso-web:3.6.1'
        

    Kotlin

        androidTestImplementation('androidx.test.espresso:espresso-web:3.6.1')
        
  3. Espresso-Web 仅与 Espresso 2.2 或更高版本以及 0.3 或更高版本的测试库兼容,因此请务必也更新这些行:

    Groovy

        androidTestImplementation 'androidx.test:runner:1.6.1'
        androidTestImplementation 'androidx.test:rules:1.6.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
        

    Kotlin

        androidTestImplementation('androidx.test:runner:1.6.1')
        androidTestImplementation('androidx.test:rules:1.6.1')
        androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')
        

常见 API 用法

onWebView() 方法是使用 Espresso 在 Android 上处理 WebView 时的主要入口点。您可以使用此方法执行 Espresso-Web 测试,例如:

Kotlin

onWebView()
    .withElement(findElement(Locator.ID, "link_2")) // similar to onView(withId(...))
    .perform(webClick()) // Similar to perform(click())

    // Similar to check(matches(...))
    .check(webMatches(getCurrentUrl(), containsString("navigation_2.html")))

Java

onWebView()
    .withElement(findElement(Locator.ID, "link_2")) // similar to onView(withId(...))
    .perform(webClick()) // Similar to perform(click())

    // Similar to check(matches(...))
    .check(webMatches(getCurrentUrl(), containsString("navigation_2.html")));

在此示例中,Espresso-Web 会找到 ID 为 "link_2" 的 DOM 元素并点击它。然后,该工具会验证 WebView 是否发送了包含 "navigation_2.html" 字符串的 GET 请求。

JavaScript 支持

执行测试时,系统会使用 JavaScript 执行所有 WebView 互动。因此,为了支持 JavaScript 评估,被测 WebView 必须启用 JavaScript。

您可以通过调用 forceJavascriptEnabled() 作为被测 activity 中的操作来强制启用 JavaScript,如以下代码段所示。

@RunWith(AndroidJUnit4::class)
class MyTestSuite {
    @get:Rule val activityScenarioRule =
        activityScenarioRule<MyWebViewActivity>()

    @Test fun testWebViewInteraction() {
        onWebView().forceJavascriptEnabled()
    }
}

常见的网页互动

与 Web.WebInteraction 对象的常见互动包括:

  • withElement() 引用 WebView 中的 DOM 元素。

    示例:

    Kotlin

    onWebView().withElement(findElement(Locator.ID, "teacher"))

    Java

    onWebView().withElement(findElement(Locator.ID, "teacher"));
  • withContextualElement() 引用 WebView 中相对于另一个 DOM 元素的作用域内 DOM 元素。您应先调用 withElement(),以建立参考 Web.WebInteraction 对象(DOM 元素)。

    示例:

    Kotlin

    .withElement(findElement(Locator.ID, "teacher"))
        .withContextualElement(findElement(Locator.ID, "person_name"))

    Java

    .withElement(findElement(Locator.ID, "teacher"))
        .withContextualElement(findElement(Locator.ID, "person_name"));
  • check() 会评估条件,确保其解析为 true。

    示例:

    Kotlin

    onWebView()
        .withElement(findElement(Locator.ID, "teacher"))
        .withContextualElement(findElement(Locator.ID, "person_name"))
        .check(webMatches(getText(), containsString("Socrates")))

    Java

    onWebView()
        .withElement(findElement(Locator.ID, "teacher"))
        .withContextualElement(findElement(Locator.ID, "person_name"))
        .check(webMatches(getText(), containsString("Socrates")));
  • perform() 在 WebView 中执行操作,例如点击某个元素。

    示例:

    Kotlin

    onWebView()
        .withElement(findElement(Locator.ID, "teacher"))
        .perform(webClick())

    Java

    onWebView()
        .withElement(findElement(Locator.ID, "teacher"))
        .perform(webClick());
  • reset() 会将 WebView 还原为初始状态。如果之前的操作(例如点击)导致导航发生变化,从而使 ElementReference 和 WindowReference 对象无法访问,则必须执行此操作。

    注意:虽然在对多页工作流(例如表单提交)进行断言时,使用 reset() 非常有用,但您的测试通常应限制在一定范围内,并专注于单个页面。

    示例:

    Kotlin

    onWebView()
        .withElement(...)
        .perform(...)
        .reset()

    Java

    onWebView()
        .withElement(...)
        .perform(...)
        .reset();

示例

以下示例测试在 WebView 中输入文本并选择提交按钮后,同一文本是否会显示在同一 WebView 中的其他元素内:

Kotlin

const val MACCHIATO = "Macchiato"

@RunWith(AndroidJUnit4::class)
class MyEspressoWebTestSuite {

    @Test fun typeTextInInput_clickButton_SubmitsForm() {
        // Create an intent that displays a web form.
        val webFormIntent = Intent()
        // ...

        // Lazily launch the Activity with a custom start Intent per test.
        ActivityScenario.launchActivity(webFormIntent)

        // Selects the WebView in your layout. If you have multiple WebView
        // objects, 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 and enter new text into the input element.
            .perform(clearElement())
            .perform(DriverAtoms.webKeys(MACCHIATO))

            // Find the "Submit" button and simulate a click using JavaScript.
            .withElement(findElement(Locator.ID, "submitBtn"))
            .perform(webClick())

            // Find the response element by ID, and verify that it contains the
            // entered text.
            .withElement(findElement(Locator.ID, "response"))
            .check(webMatches(getText(), containsString(MACCHIATO)))
    }
}

Java

public static final String MACCHIATO = "Macchiato";

@Test
public void typeTextInInput_clickButton_SubmitsForm() {
    // Create an intent that displays a web form.
    Intent webFormIntent = new Intent();
    // ...

    // Lazily launch the Activity with a custom start Intent per test.
    ActivityScenario.launchActivity(webFormIntent);

    // Selects the WebView in your layout. If you have multiple WebView objects,
    // 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 and enter new text into the input element.
        .perform(clearElement())
        .perform(DriverAtoms.webKeys(MACCHIATO))

        // Find the "Submit" button and simulate a click using JavaScript.
        .withElement(findElement(Locator.ID, "submitBtn"))
        .perform(webClick())

        // Find the response element by ID, and verify that it contains the
        // entered text.
        .withElement(findElement(Locator.ID, "response"))
        .check(webMatches(getText(), containsString(MACCHIATO)));
}

其他资源

如需详细了解如何在 Android 测试中使用 Espresso-Web,请参阅以下资源。

示例

  • WebBasicSample:使用 Espresso-Web 与 WebView 对象进行交互。