Selenium - Log4j Logging


Advertisements

Log4j is an audit logging framework that gives information about what has happened during execution. It offers the following advantages −

  • Enables us to understand the application run.

  • Log output can be saved that can be analyzed later.

  • Helps in debugging, in case of test automation failures.

  • Can also be used for auditing purposes to look at the application's health.

Components

1. Instance of Logger class.

2. Log level methods used for logging the messages as one of the following −

  • error
  • warn
  • info
  • debug
  • log

Example

Let us use the same percent calculator for this demo.

Step 1 − Download log4j JAR file from https://logging.apache.org/log4j/1.2/download.html and download the Zipped format of the JAR file.

selenium_ide_158

Step 2 − Create 'New Java Project' by navigating to the File menu.

selenium_ide_159

Step 3 − Enter the name of the project as 'log4j_demo' and click 'Next'.

selenium_ide_160

Step 4 − Click Add External Jar and add 'Log4j-1.2.17.jar'.

selenium_ide_161

Step 5 − Click Add External Jar and add Selenium WebDriver Libraries.

selenium_ide_162

Step 6 − Click Add External Jar and add Selenium WebDriver JAR's located in the Libs folder.

selenium_ide_163

Step 7 − Add a New XML file using which we can specify the Log4j properties.

selenium_ide_164

Step 8 − Enter the Logfile name as 'Log4j.xml'.

selenium_ide_165

Step 9 − The final folder structure is shown below.

selenium_ide_166

Step 10 − Now add the properties of Log4j which would be picked up during execution.

<?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 = "percent_calculator.log"/>
      
      <layout class = "org.apache.log4j.PatternLayout">
         <param name = "ConversionPattern"
            value = "%d{yyyy-MM-dd HH:mm:ss}  [%c] (%t:%x) %m%n" />
      </layout>
   </appender>

   <root>
      <level value = "INFO"/>
      <appender-ref ref = "fileAppender"/>
   </root>
   
</log4j:configuration>

Step 11 − Now for demonstration purpose, we will incorporate log4j in the same test that we have been performing (percent calculator). Add a class file in the 'Main' function.

package log4j_demo;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class log4j_demo {
   static final Logger logger = LogManager.getLogger(log4j_demo.class.getName());
   
   public static void main(String[] args) {
   
      DOMConfigurator.configure("log4j.xml");
      logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");
      logger.info("TEST Has Started");
      WebDriver driver = new FirefoxDriver();
      
      //Puts a Implicit wait, Will wait for 10 seconds before throwing exception
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
      //Launch website
      driver.navigate().to("http://www.calculator.net/");
      logger.info("Open Calc Application");
      
      //Maximize the browser
      driver.manage().window().maximize();
      
      //Click on Math Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click();
      logger.info("Clicked Math Calculator Link");
  
      //Click on Percent Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a")).click();
      logger.info("Clicked Percent Calculator Link");
      
      //Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");
      logger.info("Entered Value into First Text Box");
      
      //Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");
      logger.info("Entered Value into Second Text Box");
      
      //Click Calculate Button
      driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr/td[2]/input")).click();
      logger.info("Click Calculate Button");
      
      //Get the Result Text based on its xpath 
      String result =
         driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/span/font/b")).getText();  
      logger.info("Get Text Value");
      
      //Print a Log In message to the screen
      logger.info(" The Result is " + result);
      
      if(result.equals("5")) {
         logger.info("The Result is Pass");
      } else {
         logger.error("TEST FAILED. NEEDS INVESTIGATION");
      }
      
      logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");
      //Close the Browser.
      driver.close();    
   }
}

Execution

Upon execution, the log file is created on the root folder as shown below. You CANNOT locate the file in Eclipse. You should open 'Windows Explorer' to show the same.

selenium_ide_167

The contents of the file is shown below.

selenium_ide_168
selenium_test_design_techniques.htm
Advertisements