Logging in Selenium Tests with Log4j
Logging in Selenium Tests with Log4j
Using Log4j for logging in Selenium tests helps you track test execution flow, debug failures, and maintain detailed test reports. Log4j is a popular Java-based logging framework that integrates well with Selenium and test frameworks like TestNG or JUnit.
✅ Step-by-Step Guide to Using Log4j in Selenium Tests
1. Add Log4j Dependencies
If you use Maven, add Log4j dependencies to your pom.xml:
xml
Copy
Edit
<dependencies>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.x.x</version>
</dependency>
<!-- Log4j 2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
</dependencies>
2. Create Log4j2 Configuration File (log4j2.xml)
Place this file under src/main/resources or src/test/resources.
xml
Copy
Edit
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="FileLogger" fileName="logs/selenium-tests.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="FileLogger"/>
</Root>
</Loggers>
</Configuration>
This config logs to console and a file named selenium-tests.log.
Logs include timestamps, thread info, log level, class, line number, and message.
3. Initialize Logger in Your Test Class
java
Copy
Edit
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GoogleSearchTest {
private static final Logger logger = LogManager.getLogger(GoogleSearchTest.class);
private WebDriver driver;
@BeforeClass
public void setup() {
logger.info("Starting browser setup");
driver = new ChromeDriver();
logger.info("ChromeDriver initialized");
}
@Test
public void testGoogleSearch() {
logger.info("Navigating to Google");
driver.get("https://www.google.com");
logger.info("Page title: " + driver.getTitle());
assert driver.getTitle().contains("Google");
logger.info("Test completed successfully");
}
@AfterClass
public void teardown() {
if (driver != null) {
logger.info("Closing browser");
driver.quit();
}
}
}
4. Run Tests and Check Logs
Console output will show logs.
Detailed logs will be saved in logs/selenium-tests.log.
📝 Tips for Effective Logging
Use appropriate log levels:
logger.info() for general info
logger.debug() for detailed debugging
logger.error() for errors and exceptions
Log important actions: navigation, clicks, inputs, validations.
Log exceptions with stack traces using logger.error("message", exception).
Rotate or archive log files in production environments.
✅ Summary
Step Purpose
Add Log4j dependencies Include logging framework
Configure log4j2.xml Define log format and output targets
Initialize logger Use logger in Selenium test code
Add log statements Track test flow and issues
Learn Selenium JAVA Training in Hyderabad
Read More
Page Object Model (POM) in Selenium with Java
Integrating Selenium with Maven and Jenkins for CI/CD
Creating a Selenium Framework from Scratch Using Java and TestNG
How to Take Screenshots in Selenium Automatically on Failure
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment