Tutorials Hut

  • Selenium WebDriver

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report



  • Set up Logging using Log4j in selenium

    This article will present you with a complete idea about the logger , log4j in selenium, components of Log4j and will give an example how to set Log4j up in our project.

      • Importance of Logs in Automation Project?
      • What is Log4j in Selenium?
      • Components of Log4j
      • How to Set up Log4j in our Selenium Automation Project using log4j.properties?
      • Configure logger with log4j.xml file
    1- Set up Logging using Log 4j in selenium

    Importance of Logs in Automation Project?

    Log analysis is one of the most important aspects of performing effective and efficient software testing.

    End-to-end functionalities and UI may look right but a QA can never be sure about the product until they make sure that there are no errors or exceptions or something unexpected in the application logs.

    During the execution  of test cases, user wants some information to be logged in the console. We need the execution log which helps the user to understand the test steps or any failure during the test case execution.

    With the help of Log4j it is possible to enable loggings during the Selenium test case execution.

    What is Log4j in Selenium?

    Log4j is an open source logging framework. With log4j – logging behaviour can be controlled by editing a configuration file only without touching the application code and can be used to store the Selenium Automation flow logs.

    Log4j in selenium can be used to track the execution flow, identify problematic area effectively and easy debugging.

    Let’s take a look at the various components of the Log4j logging framework.

    Components of Log4j

    The Log4j logging framework comprises the following components:

      1. Logger
      2. Appenders
      3. Layout

    Logger

    The function of the logger in Log4j is storing and capturing all the necessary logging information that will be generated using the scripts.

    To implement loggers into a project following steps need to be performed –

    Create an instance for logger class

    Define the Log level: There are primarily five kinds of log levels

      1. DEBUG – print the debugging information .
      2. INFO – print informational message that highlights the progress of the application
      3. WARN – print information regarding faulty and unexpected system behaviour.
      4. ERROR – print error message that might allow system to continue
      5. FATAL – print system critical information which are causing the application to crash

    Appenders

    The appender basically takes information from the logger and write log messages to a file or any other storage location.

    Below are few of the appenders

      1. FileAppender –Will append the log messages to a file.
      2. RollingFileAppender –It is same as FileAppender on the basis of functionalities, but users will specify the maximum file size and once the limit is exceeded, the appender will create another file to write the messages.
      3. ConsoleAppender –Appender will write the log messages in the console.

    Layouts

    It is responsible for formatting logging information in different styles.

    How to Set up Log4j in our Selenium Automation Project?

    1. Download log4j JAR file from https://logging.apache.org/log4j/1.2/download.html
    2. Create New Java Project or add this external JAR library in any existing java project using below navigation

    Right click on src -> Build Path -> Configure Build Path -> Libraries -> Add External JARs -> “Browse the folder in which you have saved the jar files” -> Select the jar file -> Click open -> Click OK

    OR

    Add below maven dependency in pom.xml if you are using maven in the project

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>

    Reference : https://mvnrepository.com/artifact/log4j/log4j

    3.Create a file in the project name as log4j.properties and add below set of lines/code in this.

    Add this file in the Project.

    #Set the level
    log4j.rootCategory=debug, console, file
    # Appender for writing to console
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=%d{MM-dd-yyyy HH:mm:ss} %F %-5p [%t] %c{2} %L - %m%n
    # Appender for writing to a file
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=logFile.log
    # Define maximum size of the log file
    log4j.appender.file.MaxFileSize=8mb
    log4j.appender.file.MaxBackupIndex=10
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern= %d{MM-dd-yyyy HH:mm:ss} %F %-5p [%t] %c{2} %L - %m%n
    log4j.appender.file.Append=false

    In the above log4j.properties file below are few parameters defined

      1. rootCategory defines the different ways in which we want logs to be generated.
      2. ConsoleAppender : display logs on Console
      3. RollingFileAppender  :used to create and store logs in a file.
      4. Append=false:will overwrite previous logs.

    To append your current logs to previously generated logs change this value to true.

      • Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character
      • %d{dd MMM yyyy HH:mm:ss,SSS}– Used to output the date of the logging event, we can specify date format specifier enclosed between braces
      • %F– Used to print file name
      • %-5pmeans the priority of the logging event should be left justified to a width of five characters.
      • %t – Used to output the name of the thread that generated the logging event.
      • %L– Used to output the line number from where the logging request was issued.
      • %m– Used to output the application supplied message associated with the logging event.
      • %M– Used to output the method name where the logging request was issued.
      • %d{ISO8601}Formats a Date in the format “yyyy-MM-dd HH:mm:ss,SSS” for example “1999-11-27 15:49:37,459”.
      • %d{HH:mm:ss,SSS} Used to output the date of the logging event. If no date format specifier is given then ISO8601 format is assumed by default.

    Below are the steps to create your own logs in Selenium using log4j:

    1. Configure log4j.properties file using below

    PropertyConfigurator.configure(“path of log4j.properties”);

    2. Create a Logger object inside the class and Invoke getLogger() method of Logger class and pass the className.class as an argument to it.

    public static Logger log = Logger.getLogger(LoggerTestExample.class);

    3. Use this object with any of the log levels with our own text.

    Below is the code snippet for this

    package mavenLog4jProject.log4Jproject;
    
    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;
    
    public class LoggerTestExample {
    
    // get a logger instance
    public static Logger log = Logger.getLogger(LoggerTestExample.class);
    
    public void testLoggerWarn() {
    log.warn("Log test in Warn method");
    }
    
    public void testLoggerError() {
    log.error("Log test in Error method");
    }
    
    public void testLoggerDebug() {
    log.debug("Log test in Debug method");
    }
    
    public void testLoggerInfo() {
    log.info("Log test in Info method");
    }
    
    public void testLoggerFatal() {
    log.fatal("Log test in Fatal method");
    }
    
    public static void main(String[] args) {
    PropertyConfigurator.configure("log4j.properties");
    LoggerTestExample ltest = new LoggerTestExample();
    ltest.testLoggerWarn();
    ltest.testLoggerError();
    ltest.testLoggerDebug();
    ltest.testLoggerInfo();
    
    ltest.testLoggerFatal();
    }
    }
    
    

    Output:

    2- Set up Logging using Log 4j in selenium

    Configure logger with log4j.xml file

    We can also configure the log4j logging system with xml configuration file which is log4j.xml.

    3- Set up Logging using Log 4j in selenium

    In the similar way as described above , we can mention user defined logs in the class .

    Also Configure log4j.xml file using below

    DOMConfigurator.configure(“log4j.xml”);




  • Selenium WebDriver Tutorials

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report

















  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    Scroll to Top