diff --git a/test/src/main/java/com/gargoylesoftware/htmlunit/WebClientUtil.java b/test/src/main/java/com/gargoylesoftware/htmlunit/WebClientUtil.java index 9af91c19138edfbca3fbf61c95da8d662a1fc934..d8f657be3e4ac883508a1f03de8b5dd0c60c52b8 100644 --- a/test/src/main/java/com/gargoylesoftware/htmlunit/WebClientUtil.java +++ b/test/src/main/java/com/gargoylesoftware/htmlunit/WebClientUtil.java @@ -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 tom.fennelly@gmail.com */ @@ -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) { + } + } } diff --git a/test/src/test/java/hudson/ExceptionTest.java b/test/src/test/java/hudson/ExceptionTest.java index 907a2e30e792f72de8bdad2fbef6008ee8c00d40..1e68673c8293d0a274655f97a57ed20a104638b3 100644 --- a/test/src/test/java/hudson/ExceptionTest.java +++ b/test/src/test/java/hudson/ExceptionTest.java @@ -1,6 +1,8 @@ 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")); } }