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

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

在浓咖啡中有一种方法可以:
1)validation正确的URL正在启动
2)访问网站上的元素,以便我可以输入登录信息并继续登录

在这里输入图像说明

当我尝试在Espresso Launch Navigator中查看它时,没有任何东西显示在页面上,如果我尝试记录,则不会在页面上输入任何东西。

这是我到目前为止(这是在Kotlin(不是Java)): 在这里输入图像说明

这里是显示的错误: 在这里输入图像说明

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

我也试过:

在这里输入图像说明

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

更新:

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

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

1)validation正确的URL正在启动

在这里unit testing就够了。 您只需确保传递给Chrome自定义标签库的url是正确的即可。 你正在确保你的代码正常工作。 接下来发生的事情是由图书馆处理,测试属于那里。

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

在这里你正在测试一个简单的网页。 你为什么要开始模拟器的额外开销? 也许selenium或任何很酷的网络将在这里工作(而不是一个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自定义选项卡,我使用了UIAutomator:

在这里输入图像说明