在 Android 中匹配来自 Espresso 的 WebView 中的视图

Match a View in WebView from Espresso in Android

我想匹配加载 Salesforce 登录页面的 WebView 中的用户名文本字段(但也可以应用于任何其他带有文本字段的页面)。

我试过:

onView(withHint("User Name")).perform(typeText("test@sf.com"));

但这不起作用。有更好的主意吗?

您只需在 WebView 中填写 运行 js 代码即可。 例如:

webView.loadUrl("
   javascript:document.getElementById('username-field').value = 'test@sf.com’;
");

这可以使用 Espresso Web 2.2 来完成API

onWebView().withElement(findElement(Locator.ID,"username")).perform(webKeys("test@sf.com"));

如果您不知道ID,请使用Locator.Xpath。 要首先找到 xpath,您需要检查 html 代码,您可以根据该代码编写 xpath。 要获取 html 源代码,您可以使用 chrome 检查器。 将设备连接到PC,然后打开 chrome inspector.You 可以右键单击 html 代码,然后单击复制 xpath 以获取 xpath。