Friday, 11 July 2014

Log4j configuration for java(selenium) projects in eclips

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

Step2: Download Log4j from " http://mirrors.ibiblio.org/pub/mirrors/maven/log4j/jars/log4j-1.2.15.jar"
Step 3: add this jar file in to eclipse
Select your project > Right click > Click on build path> Click Configure build path> Go to library section > Add external jar file > select the log4j > click on save.
Step 4:
Open notepad and copy the below code and save the file as log4j.properties, it looks like


Step 5- Write test case

import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Google {
    public static void main(String[] args) {
      
    // Here we need to create logger instance so we need to pass Class name for which we want to create log file in my case Google is classname
         Logger logger=Logger.getLogger("Google");
        
       PropertyConfigurator.configure("Log4j.properties");

        WebDriver driver = new FirefoxDriver();
        logger.info("Browser Opened");
      
      
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        logger.info("Implicit wait given");
      
        driver.get("https://www.google.co.in/");
        logger.info("Url opened");
      
        driver.findElement(By.name("q")).sendKeys("Selenium");
        logger.info("keyword type");           
    }
}


Step 6- Run your test case and verify the output and log files as well

Here is the log files


Note: The folder structure should be like below
 MyProject
       src
           MyClass.java
           log4j.properties

No comments:

Post a Comment