问题描述: 在Android Espresso测试中,无法检测到可见文本。
解决方法:
onView
方法时,可以使用withText
或者withTextContains
来检测可见的文本。例如:onView(withText("Hello World")).check(matches(isDisplayed()));
matches(withText())
来检测可见文本。例如:onView(withId(R.id.textView)).check(matches(withText("Hello World")));
WebView
来显示文本,那么Espresso无法直接检测到这些文本。你可以尝试使用WebViewInteraction
来与WebView进行交互,并检查相关的文本。例如:onWebView()
.withElement(findElement(Locator.ID, "web_view"))
.check(webMatches(getText(), containsString("Hello World")));
performAccessibilityAction
方法中添加相关的辅助功能支持。例如:@Override
public boolean performAccessibilityAction(int action, Bundle arguments) {
// 添加辅助功能支持
if (action == AccessibilityNodeInfo.ACTION_FOCUS) {
return true;
} else if (action == AccessibilityNodeInfo.ACTION_CLICK) {
performClick();
return true;
}
return super.performAccessibilityAction(action, arguments);
}
以上是一些可能导致Espresso无法检测到可见文本的问题和解决方法。你可以根据你的具体情况选择适当的方法来解决问题。