Capture Console Logs – New feature Selenium 4

Capture​​ Console Logs​​ – New feature in​​ Selenium 4

Let us go to the site​​ https://www.selenium-tutorial.com/courses​​ 

In an application, the javascript related errors will be captured​​ as logs​​ in the ‘Console’ as shown below

We can capture these log messages by implementing the below piece of code. We enable the logs in line#19​​ and we add a listener in line#21 that will keep listening to the logs. We then print the logs

Run the script, notice below the application launched at runtime, we can see the console logs by inspecting it

Below is the console log message that gets printed

Let us consider another example with the site

https://the-internet.herokuapp.com/broken_images​​ 

Change the url and run

Some websites show console text message as well (see the black color text message in console, the red ones are js related discussed above)

To capture these console text messages, just add additional lines (line#21 to enable console, line#31 to add listener and line#33 to print the console text message)

Save an run

Notice below that console text messages too get captured

Capture logs (without using devtools):

Below strategy captures the console logs without using devTools

 

 

Code snippet​​ (capture logs​​ – method 1)

package​​ sel4scripts;

 

import​​ org.openqa.selenium.WebDriver;

import​​ org.openqa.selenium.chrome.ChromeDriver;

import​​ org.openqa.selenium.devtools.DevTools;

import​​ org.openqa.selenium.devtools.v85.log.Log;

 

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ CaptureConsoleLogs {

public​​ static​​ void​​ main(String[]​​ args) {

 WebDriverManager.chromedriver().setup();

 WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

 DevTools​​ devTools​​ = ((ChromeDriver)​​ driver).getDevTools();

devTools.createSession();

 ​​ ​​ ​​​​ 

//Enable​​ devtools​​ for logs

 ​​ ​​ ​​​​ devTools.send(Log.enable());

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ devTools.addListener(Log.entryAdded(),

 ​​ ​​ ​​​​ logEntry​​ ->

 ​​ ​​ ​​​​  {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ System.out.println("Console Log message-->"​​ +​​ logEntry.getText());

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ System.out.println("Log level message-->"​​ +​​ logEntry.getLevel());

 ​​ ​​ ​​​​  }

 ​​ ​​ ​​​​  );

 ​​ ​​ ​​​​ driver.get("https://www.selenium-tutorial.com/courses");

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​ ​​ ​​ ​​​​ 

}

}

Code snippet (capture logs – method 2, without using devTools)

package​​ sel4scripts;

 

import​​ java.util.List;

 

import​​ org.openqa.selenium.WebDriver;

import​​ org.openqa.selenium.chrome.ChromeDriver;

import​​ org.openqa.selenium.logging.LogEntries;

import​​ org.openqa.selenium.logging.LogEntry;

import​​ org.openqa.selenium.logging.LogType;

 

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ CaptureLogsSecondWay {

public​​ static​​ void​​ main(String[]​​ args) {

 WebDriverManager.chromedriver().setup();

 WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

 DevTools​​ devTools​​ = ((ChromeDriver)​​ driver).getDevTools();

devTools.createSession();

 ​​ ​​ ​​​​ 

driver.get("https://www.makemytrip.com/");

 LogEntries​​ entry​​ =​​ driver.manage().logs().get(LogType.BROWSER);

 List<LogEntry>​​ logs​​ =​​ entry.getAll();

for(LogEntry​​ l:​​ logs)

 {

 System.out.println(l.getMessage());

 }

 ​​ ​​ ​​​​ 

}

}

Code snippet (console text message)

package​​ sel4scripts;

 

import​​ org.openqa.selenium.WebDriver;

import​​ org.openqa.selenium.chrome.ChromeDriver;

import​​ org.openqa.selenium.devtools.DevTools;

import​​ org.openqa.selenium.devtools.v85.console.Console;

import​​ org.openqa.selenium.devtools.v85.log.Log;

 

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ CaptureConsoleLogs {

public​​ static​​ void​​ main(String[]​​ args) {

 WebDriverManager.chromedriver().setup();

 WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

 DevTools​​ devTools​​ = ((ChromeDriver)​​ driver).getDevTools();

devTools.createSession();

 ​​ ​​ ​​​​ 

//Enable​​ devtools​​ for logs

 ​​ ​​ ​​​​ devTools.send(Log.enable());

devTools.send(Console.enable());

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ devTools.addListener(Log.entryAdded(),

 ​​ ​​ ​​​​ logEntry​​ ->

 ​​ ​​ ​​​​  {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ System.out.println("Console Log message-->"​​ +​​ logEntry.getText());

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ System.out.println("Log level message-->"​​ +​​ logEntry.getLevel());

 ​​ ​​ ​​​​  }

 ​​ ​​ ​​​​  );

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ devTools.addListener(Console.messageAdded(),​​ message​​ ->{

 System.out.println("Console text message --> "+​​ message.getText());

 });

 ​​ ​​ ​​​​ driver.get("https://www.flipkart.com/");​​ 

 ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ 

}

}

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024