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

问题描述 投票:5回答:2

使用espresso,我们点击一​​个登录按钮,启动外部网站(Chrome自定义标签),您可以登录,然后重定向回我们的Android应用程序。

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

enter image description here

当我尝试在Espresso Launch Navigator中查看它时,没有任何内容显示在页面上,如果我尝试录制,它就不会在我页面上输入任何内容。

这是我到目前为止(它在Kotlin(不是Java)):enter image description here

以下是显示的错误:enter image description here

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

我也尝试过:

enter image description here

更新:这是使用Chrome自定义标签(而非Web视图),因此Espresso Web无法正常运行。

android android-espresso android-uiautomator chrome-custom-tabs android-espresso-recorder
2个回答
2
投票

更新:

您无法使用Espresso来测试Chrome自定义标签。 Espresso在测试您自己的应用时起作用。

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

1)验证正在启动的URL是否正确

单位测试就足够了。您只需确保传递给Chrome自定义标签库的网址是正确的。您确保您的代码正常工作。接下来发生的事情由库处理,测试属于那里。

2)访问网站上的元素,以便我可以输入登录信息并继续登录

在这里,您正在测试一个简单的网页。为什么你想要启动模拟器的额外开销?也许Selenium或任何酷的网络都可以在这里工作(不是网络开发)?

你可以使用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)));
}

2
投票

我能够使用Espresso和UI Automator解决这个问题。你可以将两者结合起来。选择我使用Espresso的登录按钮(以及应用程序的其余部分,我将使用Espresso)。要处理用于登录的Chrome自定义标签,我使用了UIAutomator:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.