Monday, 8 September 2014

How to create log file in Selenium Webdriver using log4j

Log file is simple file which keep track of the record or event or info when any event happens or any software run. This whole process know as logging.We can create log file as simple log file in .txt format.

We can create log file for our simple script also so we can track or debug our script easily if any thing goes wrong in script.

What is log4J
Log4j is open source tool given by apache for creating log files It help us to generate log file in various output target.


Steps to create log file:



   1.       Import log4j-1.2.17.jar file in your eclipse project from 

   2.       Create a folder under src. Name it as Logs.

   3.       Create a text file and rename it as Log4j.properties inside the folder.

   4.       Copy the below content into it.

  
# Define the root logger with appender file
log = D:\\Workspace\\DemoSiteAutomation\\Logs\\
log4j.rootLogger = INFO, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
# Set the name of the file
log4j.appender.FILE.File=D:\\Workspace\\DemoSiteAutomation\\Logs\\Testlog.log

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=info

# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true

# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=2

# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=51200KB

# Set the DatePattern
log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-a

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p
   %c{1}:%L - %m%n




        5. Now define the logs in your test case as shown below.

kage Testcases;

import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import CommonLibraries.*;
import org.apache.log4j.*;

public class Test_or_property {
     
      static Logger log=Logger.getLogger(Test_or_property.class);
     
      public static void main(String[] args) throws Throwable
      {                 
           
      WebDriver driver1 = new FirefoxDriver();
     
driver1.manage().window().maximize();
      driver1.get("http://demo.mahara.org/");
     
       
        log.info("Open The URL");   
   driver1.findElement(By.id(Object.getProperty("demo_username_txtbox"))).sendKeys(result[1][0]);
      driver1.findElement(By.id(Object.getProperty("demo_password_txtbox"))).sendKeys(result[1][1]);
      driver1.findElement(By.id(Object.getProperty("demo_login_btn"))).click();
     
     
        log.info("Successfully Logged in");

 

The folder structure of the project looks like :



6.    Explanation of code:

a)    Import import org.apache.log4j.*;
The above import will use log4j properties in the test case.
b)    Write the code to create a logger object named as log. Define the class name in it.
static Logger log=Logger.getLogger(Test_or_property.class);
c)    Now use the log object to write to the log file using its inbuilt method info.
   log.info("Open The URL");
   

 





 

No comments:

Post a Comment