提交 19cf13a5 编写于 作者: T tfennelly

ExceptionTest fixes

上级 bceaf9ce
......@@ -23,6 +23,13 @@
*/
package com.gargoylesoftware.htmlunit;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;
import org.junit.Assert;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
......@@ -33,6 +40,52 @@ public class WebClientUtil {
}
public static void waitForJSExec(WebClient webClient, long timeout) {
webClient.getJavaScriptEngine().processPostponedActions();
webClient.waitForBackgroundJavaScript(timeout);
}
public static ExceptionListener addExceptionListener(WebClient webClient) {
ExceptionListener exceptionListener = new ExceptionListener(webClient);
webClient.setJavaScriptErrorListener(exceptionListener);
return exceptionListener;
}
public static class ExceptionListener implements JavaScriptErrorListener {
private final WebClient webClient;
private ScriptException scriptException;
private ExceptionListener(WebClient webClient) {
this.webClient = webClient;
}
public ScriptException getScriptException() {
return scriptException;
}
public ScriptException getExpectedScriptException() {
assertHasException();
return scriptException;
}
@Override
public void scriptException(HtmlPage htmlPage, ScriptException scriptException) {
this.scriptException = scriptException;
}
public void assertHasException() {
WebClientUtil.waitForJSExec(webClient);
Assert.assertNotNull("A JavaScript Exception was expected.", scriptException);
}
@Override
public void timeoutError(HtmlPage htmlPage, long allowedTime, long executionTime) {
}
@Override
public void malformedScriptURL(HtmlPage htmlPage, String url, MalformedURLException malformedURLException) {
}
@Override
public void loadScriptError(HtmlPage htmlPage, URL scriptUrl, Exception exception) {
}
}
}
package hudson;
import com.gargoylesoftware.htmlunit.ScriptException;
import com.gargoylesoftware.htmlunit.WebClientUtil;
import org.junit.Assert;
import org.jvnet.hudson.test.HudsonTestCase;
/**
......@@ -11,14 +13,12 @@ public class ExceptionTest extends HudsonTestCase {
* Makes sure that an AJAX handler error results in a fatal problem in the unit test.
*/
public void testAjaxError() throws Exception {
try {
createWebClient().goTo("/self/ajaxError");
fail("should have resulted in a ScriptException");
} catch (ScriptException e) {
if (e.getMessage().contains("simulated error"))
return; // as expected
throw e;
WebClient webClient = createWebClient();
WebClientUtil.ExceptionListener exceptionListener = WebClientUtil.addExceptionListener(webClient);
webClient.goTo("/self/ajaxError");
}
// Check for the error.
ScriptException e = exceptionListener.getExpectedScriptException();
Assert.assertTrue(e.getMessage().contains("simulated error"));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册