Search in sources :

Example 6 with WebConsole

use of com.gargoylesoftware.htmlunit.WebConsole 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 7 with WebConsole

use of com.gargoylesoftware.htmlunit.WebConsole 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)

Example 8 with WebConsole

use of com.gargoylesoftware.htmlunit.WebConsole in project selenium_java by sergueik.

the class HtmlUnitConsole method setHtmlUnitConsole.

/**
 * Set console handler for HtmlUnitDriver.
 *
 * @param driver HtmlUnitDriver.
 */
public static void setHtmlUnitConsole(WebDriver driver) {
    try {
        Field f = driver.getClass().getDeclaredField("webClient");
        f.setAccessible(true);
        WebClient webClient = (WebClient) f.get(driver);
        WebConsole webConsole = webClient.getWebConsole();
        com.gargoylesoftware.htmlunit.WebConsole.Logger logger = webConsole.getLogger();
        if (logger != null && logger instanceof HtmlUnitConsole)
            return;
        webConsole.setLogger(INSTANCE);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        log.warn("Cannot set htmlunit console.", e);
        return;
    }
}
Also used : Field(java.lang.reflect.Field) WebConsole(com.gargoylesoftware.htmlunit.WebConsole) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 9 with WebConsole

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

the class Console method error.

/**
 * This method performs logging to the console at {@code error} level.
 * @param cx the JavaScript context
 * @param thisObj the scriptable
 * @param args the arguments passed into the method
 * @param funObj the function
 */
@JsxFunction
public static void error(final Context cx, final Scriptable thisObj, final Object[] args, final Function funObj) {
    final WebConsole webConsole = toWebConsole(thisObj);
    final Formatter oldFormatter = webConsole.getFormatter();
    webConsole.setFormatter(FORMATTER_);
    webConsole.error(args);
    webConsole.setFormatter(oldFormatter);
}
Also used : WebConsole(com.gargoylesoftware.htmlunit.WebConsole) Formatter(com.gargoylesoftware.htmlunit.WebConsole.Formatter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 10 with WebConsole

use of com.gargoylesoftware.htmlunit.WebConsole in project selenese-runner-java by vmi.

the class HtmlUnitConsole method setHtmlUnitConsole.

/**
 * Set console handler for HtmlUnitDriver.
 *
 * @param driver HtmlUnitDriver.
 */
public static void setHtmlUnitConsole(WebDriver driver) {
    try {
        Field f = driver.getClass().getDeclaredField("webClient");
        f.setAccessible(true);
        WebClient webClient = (WebClient) f.get(driver);
        WebConsole webConsole = webClient.getWebConsole();
        com.gargoylesoftware.htmlunit.WebConsole.Logger logger = webConsole.getLogger();
        if (logger != null && logger instanceof HtmlUnitConsole)
            return;
        webConsole.setLogger(INSTANCE);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        log.warn("Cannot set htmlunit console.", e);
        return;
    }
}
Also used : Field(java.lang.reflect.Field) WebConsole(com.gargoylesoftware.htmlunit.WebConsole) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Aggregations

WebConsole (com.gargoylesoftware.htmlunit.WebConsole)10 Formatter (com.gargoylesoftware.htmlunit.WebConsole.Formatter)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 Logger (com.gargoylesoftware.htmlunit.WebConsole.Logger)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 Alerts (com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts)1 RhinoException (net.sourceforge.htmlunit.corejs.javascript.RhinoException)1 Test (org.junit.Test)1