如何使用 Espresso 访问外部网站上的元素

How to access elements on external website using Espresso

使用 espresso,我们单击登录按钮启动外部网站(Chrome 自定义选项卡),您可以在其中登录,然后它会重定向回我们的 android 应用程序。

在 Espresso 中有没有办法:
1) 验证正在启动的 URL 是否正确
2)访问网站上的元素,以便我输入登录信息并继续登录

当我尝试在 Espresso Launch Navigator 中查看它时,该页面没有显示任何内容,而且如果我尝试录制,它也没有接收到我在页面上输入的任何内容。

这是我目前所拥有的(它在 Kotlin 中(不是 Java)):

这是显示的错误:

它启动我的应用程序,select 登录按钮,打开网站,但随后无法访问元素。

我也试过:

更新:这是使用 Chrome 自定义选项卡(不是 Web 视图),因此 Espresso Web 无法正常工作。

更新:

您不能使用 Espresso 测试 Chrome 自定义标签。 Espresso 在测试您自己的应用程序时有效。

要测试 Chrome 选项卡,您可以使用 UI Automator,但您可能不想这样做。

1) Verify the correct URL is being launched

单元测试就足够了。您只需要确保传递给 Chrome 自定义选项卡库的 URL 是正确的。您正在确保您的代码工作正常。接下来发生的事情由库处理,测试属于那里。

2) Access the elements on the website so that I can enter the login information and continue to login

您在这里测试一个简单的网页。为什么您想要启动模拟器的额外开销?也许 Selenium 或者任何对 web 很酷的东西都可以在这里工作(不是 web 开发人员)?

您可以使用Espresso Web

这是一个示例测试:

@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 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.
        .perform(clearElement())
        // Enter text into the input element.
        .perform(DriverAtoms.webKeys(MACCHIATO))
        // Find the submit button.
        .withElement(findElement(Locator.ID, "submitBtn"))
        // Simulate a click using 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)));
}

我使用 Espresso 和 UI Automator 解决了这个问题。您可以将两者结合起来。登录按钮的选择我使用了 Espresso(以及应用程序的其余部分,我将使用 Espresso)。为了处理用于登录的 Chrome 自定义选项卡,我使用了 UIA​​utomator: