Search in sources :

Example 1 with Logger

use of com.gargoylesoftware.htmlunit.WebConsole.Logger in project htmlunit by HtmlUnit.

the class WindowTest method dump.

/**
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = "", FF = "info: Dumper", FF_ESR = "info: Dumper")
public void dump() throws Exception {
    final WebConsole console = getWebClient().getWebConsole();
    final List<String> messages = new ArrayList<>();
    console.setLogger(new Logger() {

        @Override
        public void warn(final Object message) {
        }

        @Override
        public void trace(final Object message) {
        }

        @Override
        public void info(final Object message) {
            messages.add("info: " + message);
        }

        @Override
        public void error(final Object message) {
        }

        @Override
        public void debug(final Object message) {
        }

        @Override
        public boolean isTraceEnabled() {
            return false;
        }

        @Override
        public boolean isDebugEnabled() {
            return false;
        }

        @Override
        public boolean isInfoEnabled() {
            return true;
        }

        @Override
        public boolean isWarnEnabled() {
            return true;
        }

        @Override
        public boolean isErrorEnabled() {
            return true;
        }
    });
    final String html = "<html><head><title>foo</title><script>\n" + "function test() {\n" + "  if (window.dump) {\n" + "    window.dump('Dumper');\n" + "  }\n" + "}\n" + "</script></head><body onload='test()'></body></html>";
    loadPage(html);
    assertEquals(getExpectedAlerts(), messages);
}
Also used : WebConsole(com.gargoylesoftware.htmlunit.WebConsole) ArrayList(java.util.ArrayList) Logger(com.gargoylesoftware.htmlunit.WebConsole.Logger) Test(org.junit.Test) Alerts(com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)

Example 2 with Logger

use of com.gargoylesoftware.htmlunit.WebConsole.Logger in project htmlunit by HtmlUnit.

the class Console2Test method log.

private void log(final String logInput) throws Exception {
    final WebConsole console = getWebClient().getWebConsole();
    final List<String> messages = new ArrayList<>();
    console.setLogger(new Logger() {

        @Override
        public void warn(final Object message) {
        }

        @Override
        public void trace(final Object message) {
        }

        @Override
        public void info(final Object message) {
            messages.add("info: " + message);
        }

        @Override
        public void error(final Object message) {
        }

        @Override
        public void debug(final Object message) {
        }

        @Override
        public boolean isTraceEnabled() {
            return false;
        }

        @Override
        public boolean isDebugEnabled() {
            return false;
        }

        @Override
        public boolean isInfoEnabled() {
            return true;
        }

        @Override
        public boolean isWarnEnabled() {
            return true;
        }

        @Override
        public boolean isErrorEnabled() {
            return true;
        }
    });
    final String html = "<html><head><title>foo</title><script>\n" + "function test() {\n" + "  window.console.log(" + logInput + ");\n" + "}\n" + "</script></head><body onload='test()'></body></html>";
    loadPage(html);
    assertEquals(getExpectedAlerts(), messages);
}
Also used : WebConsole(com.gargoylesoftware.htmlunit.WebConsole) ArrayList(java.util.ArrayList) Logger(com.gargoylesoftware.htmlunit.WebConsole.Logger)

Aggregations

WebConsole (com.gargoylesoftware.htmlunit.WebConsole)2 Logger (com.gargoylesoftware.htmlunit.WebConsole.Logger)2 ArrayList (java.util.ArrayList)2 Alerts (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)1 Test (org.junit.Test)1