Using Log4j with Selenium

During the running of test case user wants some information to be logged in the console. Information could be any detail depends upon the purpose. Keeping this in mind that we are using Selenium for testing, we need the information which helps the User to understand the test steps or any failure during the test case execution. With the help of Log4j it is possible to enable loggings during the Selenium test case execution for e.g. let’s say you have encountered a failure in automation test script and it has to be reported in the system. The set of information that you have required to report a bug is :

A complete test steps to replicate the scenario
Issue, Description of the failure or reason for the failed test case
Time stamp for the developers to investigate the issue in detail
Log4j helps us to acheive the above objectives in Selenium Webdriver. When logging is wisely used, it can prove to be an essential tool.

Logging inside the Methods

Logging inside the testcase is very tedious task and sooner or later you will find it boring and annoying. Plus everybody has their own way of writing log messages and messages can be less informative and confusing. So why not make it universal. Writing logs message inside the methods is much helpful way, with that we can avoid lots of confusions, save lot of time and maintain consistency.

Steps:

1) Download JAR files of Log4j and add Jars to your project library. You can download it from apache website. That’s all about configuration of Apache POI with eclipse. Now you are ready to write your test.

2) Create a new Properties file – log4j.properties and place it under the Project src folder.

3) Paste the following code in the log4j.properties file.

# Log levels
log4j.rootLogger=INFO,CONSOLE,R
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.R.File=./logs/testlog.log
log4j.appender.R.MaxFileSize=200KB
# Number of backup files
log4j.appender.R.MaxBackupIndex=2
# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n


4) Refer below script to user log4j in your test

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class learningLog4j {

    // Declaring variable 'webDriver' of WebDriver Type
    WebDriver webDriver;

    // Declaring variable 'baseUrl' of String Type
    String baseUrl;

    // Initialize Log4j logs
    private static Logger Log = Logger.getLogger(learningLog4j.class.getName());

    @BeforeMethod
    public void startUp() throws Exception {

         Log.info("************************************* Before Method ****************************************");
       
        // Initializing FireFox Driver
        webDriver = new FirefoxDriver();

        // Assigning URL to variable 'baseUrl'
        baseUrl = "http://book.theautomatedtester.co.uk";
    }

    @Test
    public void testlearningLog4j() throws Exception {

        Log.info("****************************************Main Method************************************************");
       
        // Open the link
        webDriver.get(baseUrl);

        // Maximize browser window
        webDriver.manage().window().maximize();
    }

    @AfterMethod
    public void shutDown() throws Exception {

        Log.info("************************************** After Method **************************************************");
   
        // This will close the browser
        webDriver.quit();
    }

}


5) Once your test is complete, go to your project root/log folder and open the log file.