System.out.println() Should not be used for logging the messages. It is a PMD violation.

We have to log so many application and test specific messages for better understanding of the test execution. To achieve all these, we need to log the messages. As we are not supposed to use print statements in real time projects, it is mandatory to log everything without causing violation. Let's find out how to do that.

To perfrom the logging operations we need a jar called log4j. Download log4j here.

To demonstrate the use of log4j, let's re use the OpenGoogle class.

The below class is without the Logger implementation.

package seleniumWithTestNG;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class OpenGoogle {

    WebDriver driver;
    long startTime;
    long endTime;
    long totalTime;
    @BeforeSuite
    public void launchBrowser(){
        startTime = System.currentTimeMillis();
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");  
        // Launch the FireFox browser.
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void openGoogle(){
        driver.get("http://www.google.com");    
    }

    @AfterSuite
    public void closeBrowser(){
        driver.quit();
        endTime   = System.currentTimeMillis();
        totalTime = endTime - startTime;
        System.out.println(totalTime/1000.0+" seconds");
    }
}

Now we are going to rewrite this program with logger.

Before going to the code, you should do the following to enable the logging.

Create a log4j.xml file under your project and paste the below content in that xml file. It should like this.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
    debug="false">
    <appender name="fileAppender" class="org.apache.log4j.FileAppender">
        <param name="Threshold" value="INFO" />
        <param name="File" value="logfile.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
        </layout>
    </appender>
    <root>
        <level value="INFO" />
        <appender-ref ref="fileAppender" />
    </root>
</log4j:configuration>
package seleniumWithTestNG;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class OpenGoogle {

    WebDriver driver;
    long startTime;
    long endTime;
    long totalTime;

    static Logger log = Logger.getLogger(OpenGoogle.class);


    @BeforeSuite
    public void launchBrowser(){
        DOMConfigurator.configure("log4j.xml");

        startTime = System.currentTimeMillis();
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");  

        // Launch the FireFox browser.
        log.info("Before opening Firefox");

        driver = new FirefoxDriver();

        log.info("Firefox opened");

        driver.manage().window().maximize();
    }

    @Test
    public void openGoogle(){

        log.info("Opening google.com");

        driver.get("http://www.google.com");    
    }

    @AfterSuite
    public void closeBrowser(){

        log.info("Work done");

        driver.quit();
        endTime   = System.currentTimeMillis();
        totalTime = endTime - startTime;
        System.out.println(totalTime/1000.0+" seconds");
    }
}

These codes below are the one we have included for printing the logs.

static Logger log = Logger.getLogger(OpenGoogle.class);
DOMConfigurator.configure("log4j.xml");
log.info("Before opening Firefox"); 
log.info("Firefox opened"); 
log.info("Opening google.com"); 
log.info("Work done");

Now run your program. Once the program execution is done go to the project location in windows. if you don't remember where you have created this project in your computer, do the following.

Right click on the project -> Show in -> System Explorer

You can see a text file called logfile. Open that wth Notepad ++ or any text editor.

Homework

There are several other ways are there to enable logging with log4j, to have a better understanding, search internet and try to implement other concepts as well.

results matching ""

    No results matching ""