如何使用 espresso web select 单选值

How to select a radio value using espresso web

在我的 Android 项目中,我有一个 webview。 HTML 来源有 2 个单选按钮。使用 Espresso Web,我想从这两个选项中选择一个。我该怎么做?

例如:

<input type="radio" value="City A" name="Location" id="Location">
<input type="radio" value="City B" name="Location" id="Location">

谢谢!

将 espresso-web 添加到您的测试项目(请参阅 https://google.github.io/android-testing-support-library/docs/espresso/web/ 并按照说明确保在您的 WebView 上启用 JavaScript):

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

那么您可以对单选按钮执行操作的一种方法是使用 CSS select 或像这样:

onWebView()
  .withElement(findElement(Locator.CSS_SELECTOR, "input[value=\"City B\"]"))
  .perform(webClick());

如果您的单选按钮具有唯一 ID(就像它们应该具有的那样),您还可以 select 按 ID:

.withElement(findElement(Locator.ID, "Location2"))

查看此 https://github.com/googlesamples/android-testing/tree/master/ui/espresso/WebBasicSample 示例项目,您可以在其中查看完整的工作设置并轻松尝试不同的 HTML 代码和 selectors.