Eldy
Eldy

Reputation: 1096

Web Driver returns an empty list when trying to access console log entries

I am running some tests in Selenide, and I need to display the content of the browser console at some point:

LogEntries logs = getWebDriver().manage().logs().get(LogType.BROWSER);

System.out.println(logs.getAll().size());
for (LogEntry log: logs.getAll()) {
    System.out.println(log.getMessage());
}

However, I am getting an empty list as a result (the only thing that get logged is 0). I checked manually that the console should not be empty at this point when performing each step of my test.

For information, this is how I create my Web Driver:

private static void setUpPhantomJS() {
    Configuration.browser = "phantomjs";

    DesiredCapabilities caps = DesiredCapabilities.phantomjs();
    caps.setJavascriptEnabled(false);
    caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
                Arrays.asList("--ignore-ssl-errors=true", "--webdriver-loglevel=INFO"));
    caps.setCapability(CapabilityType.SUPPORTS_ALERTS, true);

    PhantomJSDriver webDriver = new PhantomJSDriver(caps);
    webDriver.setLogLevel(Level.ALL);
    WebDriverRunner.setWebDriver(webDriver);    
}

Why am I getting an empty list as a result?

Upvotes: 0

Views: 675

Answers (3)

dansalmo
dansalmo

Reputation: 11704

Another possible cause for receiving an empty list is that console log has been read previously. When I read the log in Ruby / Capybara in the following way:

page.driver.browser.manage.logs.get(:browser)

I get the log, but if I read it again I get an empty list until the page is refreshed.

Upvotes: 0

yong
yong

Reputation: 13722

From the webdriver logging module, you will found below NOTE:

Only a few browsers support the remote logging API (notably Firefox and Chrome). Firefox supports basic logging functionality, while Chrome exposes robust performance logging options. Remote logging is still considered a non-standard feature, and the APIs exposed by this module for it are non-frozen. This module will be updated, possibly breaking backwards-compatibility, once logging is officially defined by the W3C WebDriver spec.

It seems PhantomJS does not support remote logging API.

Upvotes: 1

Murthi
Murthi

Reputation: 5347

As yong said, phantom js doesn't support remote logging. But you can get the phantom js log file in the default location or you can set the log file path with command line argument.

capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
new String[] { 
       "--web-security=no", 
       "--ignore-ssl-errors=yes",
       "--webdriver-logfile="usr/local/logs/phantomjsdriver.log"
});

otherwise you can set the system property 'phantomjs.logfile.path'

System.setProerty("phantomjs.logfile.path',<file path>);

By default the log file is available on phantomjs directory with name phantomjs.log

Upvotes: 0

Related Questions