Java Code Examples for com.gargoylesoftware.htmlunit.WebClient#setJavaScriptErrorListener()

The following examples show how to use com.gargoylesoftware.htmlunit.WebClient#setJavaScriptErrorListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavascriptErrorListenerTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Listener should capture script exception.
 * @throws Exception if the test fails
 */
@Test
public void listenForScriptException() throws Exception {
    final WebClient webClient = getWebClient();
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    final CollectingJavaScriptErrorListener javaScriptErrorListener = new CollectingJavaScriptErrorListener();
    webClient.setJavaScriptErrorListener(javaScriptErrorListener);

    // test script exception
    final String html = "<html><head><title>Throw JavaScript Error</title>"
        + "<script>unknown.foo();</script></head>"
        + "<body></body></html>";
    loadPage(html);

    assertEquals("", javaScriptErrorListener.getWarnings());
    assertEquals("com.gargoylesoftware.htmlunit.ScriptException: "
            + "ReferenceError: \"unknown\" is not defined. "
            + "(script in http://localhost:" + PORT + "/ from (1, 58) to (1, 81)#1)",
            javaScriptErrorListener.getScriptExceptions());
    assertEquals("", javaScriptErrorListener.getLoadScriptErrors());
    assertEquals("", javaScriptErrorListener.getMalformedScriptURLErrors());
    assertEquals("", javaScriptErrorListener.getTimeoutErrors());
}
 
Example 2
Source File: JavascriptErrorListenerTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Listener should capture script exception.
 * @throws Exception if the test fails
 */
@Test
public void listenForMalformedScriptUrl() throws Exception {
    final WebClient webClient = getWebClient();
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    final CollectingJavaScriptErrorListener javaScriptErrorListener = new CollectingJavaScriptErrorListener();
    webClient.setJavaScriptErrorListener(javaScriptErrorListener);

    final String html = "<html>\n"
            + "<head><title>Throw JavaScript Error</title>\n"
            + "<script src='unknown://nowhere' type='text/javascript'></script>\n"
            + "</head>\n"
            + "<body></body>\n"
            + "</html>";

    loadPage(html);

    assertEquals("", javaScriptErrorListener.getWarnings());
    assertEquals("", javaScriptErrorListener.getScriptExceptions());
    assertEquals("", javaScriptErrorListener.getLoadScriptErrors());
    assertEquals("unknown://nowhere, java.net.MalformedURLException: unknown protocol: 'unknown'",
                javaScriptErrorListener.getMalformedScriptURLErrors());
    assertEquals("", javaScriptErrorListener.getTimeoutErrors());
}
 
Example 3
Source File: JavascriptErrorListenerTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10_000)
public void listenForTimeoutError() throws Exception {
    final WebClient webClient = getWebClient();
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    final CollectingJavaScriptErrorListener javaScriptErrorListener = new CollectingJavaScriptErrorListener();
    webClient.setJavaScriptErrorListener(javaScriptErrorListener);
    webClient.setJavaScriptTimeout(100);

    final String html = "<html><head><title>Throw JavaScript Timeout Error</title>\n"
        + "<script>while(1) {}</script></head>\n"
        + "<body></body></html>";

    loadPage(html);

    assertEquals("", javaScriptErrorListener.getWarnings());
    assertEquals("", javaScriptErrorListener.getScriptExceptions());
    assertEquals("", javaScriptErrorListener.getLoadScriptErrors());
    assertEquals("", javaScriptErrorListener.getMalformedScriptURLErrors());
    assertEquals("Timeout allowed: 100", javaScriptErrorListener.getTimeoutErrors());
}
 
Example 4
Source File: JavascriptErrorListenerTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Listener should capture script exception.
 * @throws Exception if the test fails
 */
@Test
public void listenForLoadScriptError() throws Exception {
    final WebClient webClient = getWebClient();
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    final CollectingJavaScriptErrorListener javaScriptErrorListener = new CollectingJavaScriptErrorListener();
    webClient.setJavaScriptErrorListener(javaScriptErrorListener);

    getMockWebConnection().setDefaultResponse("", 500, "Server Error", MimeType.TEXT_HTML);

    final String html = "<html><head>\n"
        + "<script src='notExisting.js' type='text/javascript'></script></head>\n"
        + "<body></body></html>";

    try {
        loadPage(html);
        fail("FailingHttpStatusCodeException expected");
    }
    catch (final FailingHttpStatusCodeException e) {
        // expected
    }

    assertEquals("", javaScriptErrorListener.getScriptExceptions());
    assertEquals(URL_FIRST + "notExisting.js, "
            + "com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: "
            + "500 Server Error for " + URL_FIRST + "notExisting.js",
            javaScriptErrorListener.getLoadScriptErrors());
    assertEquals("", javaScriptErrorListener.getMalformedScriptURLErrors());
    assertEquals("", javaScriptErrorListener.getTimeoutErrors());
}
 
Example 5
Source File: JavascriptErrorListenerTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Test for running without a JavaScript error listener.
 *
 * @throws Exception if the test fails
 */
@Test
public void nullJavaScriptErrorListener() throws Exception {
    final WebClient webClient = getWebClient();
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.setJavaScriptErrorListener(null);
    final MockWebConnection webConnection = new MockWebConnection();
    final String errorContent = "<html><head><title>ERROR 500</title></head><body></body></html>";
    final List<NameValuePair> emptyList = Collections.emptyList();
    webConnection.setResponse(URL_SECOND, errorContent, 500, "BOOM", MimeType.TEXT_HTML, emptyList);

    // test script exception
    String content = "<html><head><title>Throw JavaScript Error</title>\n"
        + "<script>unknown.foo();</script></head>\n"
        + "<body></body></html>";
    webConnection.setResponse(URL_FIRST, content);
    webClient.setWebConnection(webConnection);
    webClient.getPage(URL_FIRST);

    // test load script error
    content = "<html><head><title>Throw JavaScript Error</title>\n"
        + "<script src='" + URL_SECOND + "' type='text/javascript'></script></head>\n"
        + "<body></body></html>";
    webConnection.setResponse(URL_FIRST, content);
    try {
        webClient.getPage(URL_FIRST);
        fail("FailingHttpStatusCodeException expected");
    }
    catch (final FailingHttpStatusCodeException e) {
        // expected
    }

    // test malformed script url error
    content = "<html><head><title>Throw JavaScript Error</title>\n"
        + "<script src='unknown://nowhere' type='text/javascript'></script></head>\n"
        + "<body></body></html>";
    webConnection.setResponse(URL_FIRST, content);
    webClient.getPage(URL_FIRST);

    // test timeout error
    webClient.setJavaScriptTimeout(100);

    content = "<html><head><title>Throw JavaScript Timeout Error</title>\n"
        + "<script>while(1) {}</script></head>\n"
        + "<body></body></html>";
    webConnection.setResponse(URL_FIRST, content);
    webClient.getPage(URL_FIRST);
}