• Selenium Video Tutorials

Selenium - LogExpert Logging



Selenium Webdriver can be used for logging information during test execution. The logging is mainly used to extract information about how the execution took place.

Logging helps to enhance any application by supplying data on the features of the application which are working or not working. Thus logging is important to raise the quality of the application and troubleshoot issues.

Why Use Logging in Selenium?

The logging is an important step while writing tests in Selenium because of the below reasons −

  • Logging helps in debugging failures at a faster rate. Also, if the logging levels are set then it is easier to categorize failures.

  • Most of the logging frameworks are free, and open-source where we can set and suppress logs at various levels and bring out the optimal performance of an application.

Different Logging Levels

The first step towards logging comes with enabling logging with default settings with respect to each class. Let us first enable the default logging, which is applicable to all loggers using the root logger.

Logger logger = Logger.getLogger("");

Since logging is set with respect to a class, we can set the logging level applicable to a class.

((RemoteWebDriver) driver).setLogLevel(Level.INFO);
Logger.getLogger(SeleniumManager.class.getName())
.setLevel(Level.SEVERE);

There are a total seven log levels - SEVERE, WARNING, INFO, CONFIG, FINE, FINER, and FINEST. The INFO log level is the default one which means there are no action items to be taken care of in our code and purely used for informational purposes.

Logger logger = Logger.getLogger("");
logger.setLevel(Level.INFO);

Always, all logging levels are not required, so we can filter out the logs based on the levels using the setLevel() method.

Logger logger = Logger.getLogger("");
logger.setLevel(Level.WARNING);

Example

In the above example, log level of WARNING has been set, meaning that some actions need to be taken care, specifically for using deprecated versions in our code.

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

public class LoggingLvl {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // enabling log levels to Warning
      ((RemoteWebDriver) driver).setLogLevel(Level.WARNING);

      //adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // getting current URL
      System.out.println("Getting the Current URL: " + driver.getCurrentUrl());

      // quitting the browser
      driver.quit();
   }
}

Output

Feb 15, 2024 4:41:42 PM
 org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executing: setTimeout
 [510e0fcbc35b47f7637445d1b69bedc2, setTimeout {implicit=12000}]
Feb 15, 2024 4:41:42 PM
 org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executed: setTimeout (Response: SessionID:
 510e0fcbc35b47f7637445d1b69bedc2, Status: 0, Value: null)
Feb 15, 2024 4:41:42 PM
 org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executing: get [510e0fcbc35b47f7637445d1b69bedc2, get {url=https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php}]
Feb 15, 2024 4:41:43 PM 
org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executed: get (Response: SessionID:
 510e0fcbc35b47f7637445d1b69bedc2, Status: 0, Value: null)
Feb 15, 2024 4:41:43 PM
 org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executing: getCurrentUrl
 [510e0fcbc35b47f7637445d1b69bedc2, getCurrentUrl {}]
Feb 15, 2024 4:41:43 PM
 org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executed: getCurrentUrl (Response: SessionID:
510e0fcbc35b47f7637445d1b69bedc2, Status: 0, Value: https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php)
Feb 15, 2024 4:41:43 PM
 org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executing: quit [510e0fcbc35b47f7637445d1b69bedc2, quit {}]
Getting the Current URL: https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php
Feb 15, 2024 4:41:43 PM
 org.openqa.selenium.remote.RemoteWebDriver log
WARNING: Executed: quit (Response: SessionID: 510e0fcbc35b47f7637445d1b69bedc2, Status: 0, Value: null)

Process finished with exit code 0

In the above example, we have obtained the WARNING log levels along with the browser title with the message as Getting the Current URL: https://www.tutorialspoint.com/selenium/.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

The logging information also contains debug information for looking into a specific issue and correcting it. This can be done by setting the log level to FINE.

Logger logger = Logger.getLogger("");
logger.setLevel(Level.FINE);

Example

Let us take another example, where we would set the log level to SEVERE.

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

public class LoggingLvls {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // enabling log levels to SEVERE
      ((RemoteWebDriver) driver).setLogLevel(Level.SEVERE);

      //adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Identify the input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='name']"));

      // enter text in input box
      e.sendKeys("Selenium");

      // getting current URL
      System.out.println("Getting the Current URL: " + driver.getCurrentUrl());

      //quitting the browser
      driver.quit();
   }
} 

Output

Feb 15, 2024 4:48:27 PM org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executing: setTimeout [ca1196b6a589eeac011ff53fbc3312aa, setTimeout {implicit=12000}]
Feb 15, 2024 4:48:27 PM org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executed: setTimeout (Response: SessionID: ca1196b6a589eeac011ff53fbc3312aa, Status: 0, Value: null)
Feb 15, 2024 4:48:27 PM org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executing: get [ca1196b6a589eeac011ff53fbc3312aa, get {url=https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php}]
Feb 15, 2024 4:48:28 PM org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executed: get (Response: SessionID: ca1196b6a589eeac011ff53fbc3312aa, Status: 0, Value: null)
Feb 15, 2024 4:48:28 PM
 org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executing: findElement [ca1196b6a589eeac011ff53fbc3312aa, findElement {using=xpath, value=//*[@id='name']}]
Feb 15, 2024 4:48:28 PM
 org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executed: findElement (Response: SessionID: ca1196b6a589eeac011ff53fbc3312aa, Status: 0, Value: {element-6066-11e4-a52e-4f735466cecf=B6679A9C1F8F9717E6BD42C9A8EC529E_element_15})
Feb 15, 2024 4:48:28 PM
 org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executing: sendKeysToElement [ca1196b6a589eeac011ff53fbc3312aa, sendKeysToElement {id=B6679A9C1F8F9717E6BD42C9A8EC529E_element_15, value=[Ljava.lang.CharSequence;@4c51bb7}]
Feb 15, 2024 4:48:28 PM
 org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executed: sendKeysToElement (Response: SessionID: ca1196b6a589eeac011ff53fbc3312aa, Status: 0, Value: null)
Feb 15, 2024 4:48:28 PM
 org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executing: getCurrentUrl [ca1196b6a589eeac011ff53fbc3312aa, getCurrentUrl {}]
Feb 15, 2024 4:48:28 PM
 org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executed: getCurrentUrl (Response: SessionID: ca1196b6a589eeac011ff53fbc3312aa, Status: 0, Value: https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php)
Feb 15, 2024 4:48:28 PM
 org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executing: quit [ca1196b6a589eeac011ff53fbc3312aa, quit {}]
Getting the Current URL:
 https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php
Feb 15, 2024 4:48:28 PM 
org.openqa.selenium.remote.RemoteWebDriver log
SEVERE: Executed: quit (Response: SessionID: ca1196b6a589eeac011ff53fbc3312aa, Status: 0, Value: null)

Process finished with exit code 0

In the above example, we have obtained the SEVERE log levels along with the browser title with the message as Getting the Current URL: https://www.tutorialspoint.com/selenium/.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

The logs generated in the console can be written to another file by using a handler. All the logs are placed in System.err by default.

Example

package org.example;

import org.openqa.selenium.WebDriver;
import java.io.IOException;
import java.util.logging.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.logging.Handler;

public class LoggingLvelFile {
   public static void main(String[] args) throws InterruptedException, IOException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // enabling log levels to WARNING
      Logger logger = Logger.getLogger("");
      logger.setLevel(Level.WARNING);

      //  output logging to another file Logs1.xml
      Handler handler = new FileHandler("Logs1.xml");
      logger.addHandler(handler);

      //adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // getting current URL
      System.out.println("Getting the Current URL: " + driver.getCurrentUrl());

      //quitting the browser
      driver.quit();
   }
}

Output

Getting the Current URL: 
https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php

Process finished with exit code 0

In the above example, we retrieved the browser title with the message in the console - Getting the Current URL:

https://www.tutorialspoint.com/selenium/practice/.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Also, a Logs1.xml file would get created in the project directory with the logs.

This concludes our comprehensive take on the tutorial on Selenium - LogExpert Logging. We’ve started with describing why we use logging in Selenium and walked through different logging levels with an example illustrating how to use it along with Selenium. This equips you with in-depth knowledge of the LogExpert logging. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements