How To Generate And Insert Log In Selenium Webdriver Using log4j Jar

Assume you have prepared test case for your software web application test scenario (using selenium webdriver with eclipse and junit) and now you wants generate log file to insert your execution time log messages in to it. How you will do it? Selenium webdriver software testing tool don't have any built in function or method to generate log file. In this case you can use apache logging service. Let me describe you steps to generate log file in selenium webdriver test for software web application.

Steps To Generate And Insert Log In Selenium Webdriver

1. Download log4j jar file from logging.apache.org
  • Click Here to go to log4j jar file downloading page. (This Link URL may change in future).
  • Click on Zip folder link as shown in bellow image. Current version of log4j jar file is log4j-1.2.17. Version of log4j jar file may be different in future due to the frequent version change.
  • When you click on zip folder link, it will show you zip mirror links to download folder. Download and save zip folder by clicking on mirror link.
  • Now Extract that zip folder and look inside that extracted folder. You will find log4j-1.2.17.jar file in it.
2. Import log4j-1.2.17.jar file in your eclipse project from 
  • Right click on your project folder and go to Build Path -> Configure Build path. It will open java build path window as shown in bellow image.
  • Click on Add External JARs button and select log4j-1.2.17.jar file. It will add log4j-1.2.17.jar file in your build path.
  • Click on OK button to close java build path window.
3. Create Properties folder under your project folder and add log4j.properties file in it.
  • Create folder with name = Properties under your project by right clicking on your project folder.
  • Click Here to download log4j.properties file and save it in Properties folder which is created under your project folder.
  • Now your properties folder under project folder will looks like bellow.
     
4. Set Run Configuration
This is optional step. You need to set your Run configure if log file is not generated and display bellow given warning message in your console when you run your test case. 
log4j:WARN No appenders could be found for logger (dao.hsqlmanager).
  • Go to Run -> Run Configurations -> Classpath
  • Select User Entries -> Click on 'Advanced' button -> Select Add Folder -> And then select Properties folder from your project and click on OK. Now your Class tab will looks like bellow.
5. Create example(Sample) test case under your project.
Create your own test case or copy example test case from here and paste it in your class file.

6. Import log4j header file in your project
Import bellow given header file in your test case class file.
import org.apache.log4j.*;

7. Replace bellow given syntax in your class file and Run your test case.
@Test
  public void test () throws InterruptedException 
  {  
  Logger log;
  driver.findElement(By.id("text1")).sendKeys("My First Name");
  log = Logger.getLogger(Mytesting.class);
  log.info("Type In Text field.");
  Select mydrpdwn = new Select(driver.findElement(By.id("Carlist")));
  mydrpdwn.selectByVisibleText("Audi");
  log = Logger.getLogger(Mytesting.class);
  log.info("Select value from drop down.");
  WebDriverWait wait = new WebDriverWait(driver, 15);
  wait.until(ExpectedConditions.elementToBeClickable(By.id("text2")));
  }

When your test case for software web application is executed, Go to folder = C:\Logs. log.log file will be created over there. Open that file and look in to it. Log written in your software test case will be inserted in your log file. In above example, Syntax marked with Pink color are written for inserting log. Now you can write log at any place in your test case of software web application as shown in above example.

15 comments:

  1. There is a new version. What are we supposed to import for "apache-log4j-2.0-rc1-bin"?

    ReplyDelete
  2. Hi, After following al steps I am getting following error message

    lease help me in this regard

    log4j:WARN No such property [.MaxBackupIndex] in org.apache.log4j.RollingFileAppender.
    log4j:WARN No such property [datePattern] in org.apache.log4j.RollingFileAppender.

    regards,

    Vikash

    ReplyDelete
    Replies
    1. Hi,
      Even I get the same warnings but the script is executed successfully.

      Also, I am not able to fine the log file in C:\Log

      warning message:
      log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
      log4j:WARN Please initialize the log4j system properly.
      log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

      Regards,

      Delete
    2. Hi,

      to suppress the warning "og4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
      log4j:WARN Please initialize the log4j system properly.
      log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.", execute the steps given in Step-4 of the post (mine got resolved).

      For the properties file warning, I did the following:

      For "log4j:WARN No such property [.MaxBackupIndex] in org.apache.log4j.RollingFileAppender."
      [Sollution] - Open the log4j.properties file and remove the extra '.' from "log4j.appender.FILE..MaxBackupIndex=2".

      For "log4j:WARN No such property [datePattern] in org.apache.log4j.RollingFileAppender."
      [Sollution] - Comment out the datePattern setting by prefixing a # with setting "log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-a"

      Hope this helps.

      Delete
    3. It is not working

      Pls provide the solution to solve the issue

      Delete
  3. Hi,
    log = Logger.getLogger(Mytesting.class);
    Which Class file we should use here?

    ReplyDelete
    Replies
    1. Your class name where you are initializing this log

      Delete
  4. 1. Search with any term on Flipkart
    2. Print the product name with highest number of ratings and the star rating for that product
    3. If multiple products having the highest rating print all those products along with star points

    ReplyDelete
  5. i didn't find any fine in C:\Log.Even there is not Log folder inn C

    ReplyDelete
  6. Try this as your first statement before using loggers
    BasicConfigurator.configure();

    ReplyDelete
  7. Hi, 3 point Click Here link to download log4j.properties file is not working, can some one else also confirm the same.

    Regards
    Srikanth

    ReplyDelete
  8. Hello i want that a file containing username and password will run on the login screen through script.
    can you please tell how can i do the same.

    ReplyDelete
  9. which package to include for Logger.getLogger method
    Eclipse is showing this error
    "The method getLogger(Class) is undefined for the type Logger"

    ReplyDelete
  10. I also getting the same issue i have corrected the "log4j.appender.FILE.MaxBackupIndex=2"
    but still i am getting the Below warnings:
    log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

    Can any one please helo with this??

    ReplyDelete