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.
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);
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.
Example 1 - Warning Log Levels
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: Selenium Automation Practice Form.
Example 2 - Fine Log Levels
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 3 - Severe Log Levels
Let us take another example, where we would set the log level to SEVERE.
Code Implementation with SEVERE log levels.
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
In the above example, we have obtained the SEVERE log levels along with the browser title with the message as Getting the Current URL: Selenium Automation Practice Form.
Obtain Log Levels in Separate Files
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: Selenium Automation Practice Form.
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.
Conclusion
This concludes our comprehensive take on the tutorial on Selenium WebDriver Logging & LogExpert. Weve 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 youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.