我正在尝试获取 WebView
的实例,以便我可以对其调用 evaluateJavascript()
。
我编写了一个自定义匹配器,然后尝试将 WebView
分配给静态变量,如下所示:
public static WebView view1;
public static Matcher<View> isJavascriptEnabled1() {
return new BoundedMatcher<View, WebView>(WebView.class) {
@Override
public void describeTo(Description description) {
description.appendText("WebView with JS enabled");
}
@Override
public boolean matchesSafely(WebView webView) {
view1 =webView;
return webView.getSettings().getJavaScriptEnabled();
}
};
}
在我的测试课上,我调用:
CustomMatcher.isJavascriptEnabled1();
CustomMatcher.view1.evaluateJavascript("$('.status dl dd').get(0).innerText.replace('pts.', '').replace(',', '').trim()\"]",new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
Log.d("LogName", s); // Prints asd
}
});
我收到错误:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.evaluateJavascript(java.lang.String, android.webkit.ValueCallback)' on a null object reference
最佳答案
您没有使用 Matcher
来匹配 View 。它不起作用,因为调用 isJavascriptEnabled1()
只会创建自定义 Matcher
的新实例。但是例如matchesSafely()
永远不会执行,这就是为什么 CustomMatcher.view1
为 null
。
要使您的代码正常工作,您必须将 Espresso.onView()
与您的 Matcher
一起使用:
// Use onView and the matcher to find a web view with enabled java script
Espresso.onView(CustomMatcher.isJavascriptEnabled1());
// Now CustomMatcher.view1 should not be null (if there is a web view)
CustomMatcher.view1.evaluateJavascript("$('.status dl dd').get(0).innerText.replace('pts.', '').replace(',', '').trim()\"]",new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
Log.d("LogName", s); // Prints asd
}
});
但这仍然不正确。 evaluateJavascript()
方法是异步的,Espresso 不会等待回调 (onReceiveValue()
) 被调用。因此,测试很可能会在调用 onReceive()
之前完成。
还有Espresso Web用于测试 WebViews
。根据您的目标,您可能会发现它很有用。 Espresso Web 也在后台执行 java 脚本。
如果不使用 Espresso Web,我建议编写一个自定义 ViewAction
,它将在匹配的 WebView
上执行。此操作可以使用 WebView
来评估 java 脚本:
/**
* {@link ViewAction} that evaluates javascript in a {@link WebView}.
*/
public class EvaluateJsAction implements ViewAction, ValueCallback<String> {
private static final long TIME_OUT = 5000;
private final String mJsString;
private final AtomicBoolean mEvaluateFinished = new AtomicBoolean(false);
public EvaluateJsAction(final String javaScriptString) {
mJsString = javaScriptString;
}
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(WebView.class);
}
@Override
public String getDescription() {
return "evaluate '" + mJsString + "' on webview";
}
@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadUntilIdle();
final WebView webView = (WebView) view;
webView.evaluateJavascript(mJsString, this);
final long timeOut = System.currentTimeMillis() + TIME_OUT;
while (!mEvaluateFinished.get()) {
if (timeOut < System.currentTimeMillis()) {
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new RuntimeException(String.format(Locale.US,
"Evaluating java script did not finish after %d ms of waiting.", TIME_OUT)))
.build();
}
uiController.loopMainThreadForAtLeast(50);
}
}
@Override
public void onReceiveValue(String value) {
mEvaluateFinished.set(true);
}
}
然后使用这个 Action :
onView(withJavaScriptEnabled()).perform(new EvaluateJsAction(theJavaScriptString));
关于android - Espresso : How to call evaluateJavascript() on a WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38380246/