Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain native logger in Selenium WebDriver

Is it possible to obtain the logger somehow that Selenium WebDriver uses? I want to capture a transcript of all the commands that were issued (eg: open, wait, click, etc). In particular I am looking for a java solution, as I am exporting the tests into junit.

I found this code on their website, however it displays nothing on standard out

    DesiredCapabilities caps = DesiredCapabilities.firefox(); 
    LoggingPreferences logs = new LoggingPreferences(); 
    logs.enable(LogType.DRIVER, Level.FINEST); 
    caps.setCapability(CapabilityType.LOGGING_PREFS, logs); 
    driver = new FirefoxDriver(caps);
like image 714
Zombies Avatar asked Nov 03 '12 00:11

Zombies


People also ask

Does Selenium have Excel reading method Log4j?

Selenium Webdriver is a great tool to automate web-based applications. But it does not support read and write operations on excel files.

What is Log4j in testing?

Log4j is an audit logging framework that gives information about what has happened during execution. It offers the following advantages − Enables us to understand the application run. Log output can be saved that can be analyzed later. Helps in debugging, in case of test automation failures.


2 Answers

Enable logging in the driver you're using, select which log types you're interested in and the log level (I'm using FirefoxDriver, enabling all types of logs and collecting all log messages)

LoggingPreferences logs = new LoggingPreferences();
logs.enable(LogType.BROWSER, Level.ALL);
logs.enable(LogType.CLIENT, Level.ALL);
logs.enable(LogType.DRIVER, Level.ALL);
logs.enable(LogType.PERFORMANCE, Level.ALL);
logs.enable(LogType.PROFILER, Level.ALL);
logs.enable(LogType.SERVER, Level.ALL);

DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);

WebDriver driver = new FirefoxDriver(desiredCapabilities);

Then, after running the test you can collect the logs (I'm only collecting the DRIVER logs, but you can do the same for any type of log)

Logs logs = driver.manage().logs();
LogEntries logEntries = logs.get(LogType.DRIVER);

for (LogEntry logEntry : logEntries) {
    System.out.println(logEntry.getMessage());
}
like image 145
Miguel Ferreira Avatar answered Sep 29 '22 10:09

Miguel Ferreira


Try

driver.manage().logs()

You will get Logs interface that has methods to get logs and log types. See Logs interface docs

like image 3
Nikolay Avatar answered Sep 29 '22 12:09

Nikolay