What is the procedure of writing Selenium Test Cases with Log4j logging?

0 votes
May 22 in Selenium by Neetu
15 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

@Neetu, you can use these steps to write your Selenium Test Cases with Log4j logging:

  1. Create a new XML file – log4j.xml and place it under the Project root folder and Paste the following code in the log4j.xml file:
    • <?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>
  2. Now include this logging code in to your test script:
    • package automationTestLog4j;
      
      import java.util.concurrent.TimeUnit;
      
      import org.apache.log4j.Logger;
      
      import org.apache.log4j.xml.DOMConfigurator;
      
      import org.openqa.selenium.By;
      
      import org.openqa.selenium.WebDriver;
      
      import org.openqa.selenium.firefox.FirefoxDriver;
      
      public class Log4j {
      
          private static WebDriver driver;
      
          private static Logger Log = Logger.getLogger(Log4j.class.getName());
      
          public static void main(String[] args) {
      
              DOMConfigurator.configure("log4j.xml");
      
              // Create a new instance of the Firefox driver
      
              driver = new FirefoxDriver();
      
              Log.info("New driver instantiated");
      
              //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
      
              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
              Log.info("Implicit wait applied on the driver for 10 seconds");
      
              //Launch the Online Store Website
      
              driver.get("http://www.onlinestore.toolsqa.com");
      
              Log.info("Web application launched");
      
              // Find the element that's ID attribute is 'account'(My Account)
      
              driver.findElement(By.id("account")).click();
      
              Log.info("Click action performed on My Account link");
      
              // Find the element that's ID attribute is 'log' (Username)
      
              // Enter Username on the element found by above desc.
      
              driver.findElement(By.id("log")).sendKeys("testuser_1");
      
              Log.info("Username entered in the Username text box");
      
              // Find the element that's ID attribute is 'pwd' (Password)
      
              // Enter Password on the element found by the above desc.
      
              driver.findElement(By.id("pwd")).sendKeys("Test@123");
      
              Log.info("Password entered in the Password text box");
      
              // Now submit the form. WebDriver will find the form for us from the element
      
              driver.findElement(By.id("login")).click();
      
              Log.info("Click action performed on Submit button");
      
              // Print a Log In message to the screen
      
              System.out.println(" Login Successfully, now it is the time to Log Off buddy.");
      
               // Find the element that's ID attribute is 'account_logout' (Log Out)
      
              driver.findElement(By.id("account_logout"));
      
              Log.info("Click action performed on Log out link");
      
              // Close the driver
      
              driver.quit();
      
              Log.info("Browser closed");
      
          }
      
      }
  3. Check the output file “logfile.txt”. The output will look like below:

answered May 22 by Naveen

Related Questions In Selenium

0 votes
1 answer

What is the use of AutoIt tool with Selenium?

Hey Badrish, AutoIT is a third party tool ...READ MORE

answered May 31 in Selenium by Anvi
• 8,840 points
16 views
0 votes
1 answer

What is the role of TestNG & JUnit frameworks in Selenium?

Well TestNG and JUnit are test frameworks ...READ MORE

answered Apr 18, 2018 in Selenium by king_kenny
• 3,590 points
158 views
0 votes
2 answers
0 votes
1 answer

what is the need of xpath when you have attributes like id ,class,name in selenium?

It is not necessary that every element ...READ MORE

answered Jun 1, 2018 in Selenium by Vinita
• 220 points
68 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
167 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
557 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
152 views
0 votes
1 answer

What is the use of getOptions() method in Selenium?

Hi Urmila, getOptions() is used to get all ...READ MORE

answered May 31 in Selenium by Abha
• 11,040 points
22 views
0 votes
2 answers

What are the limitations of Selenium?

Hi Moray, limitations of selenium webdriver are ...READ MORE

answered Jun 10 in Selenium by Abha
• 11,040 points
73 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.