130 likes | 141 Views
In this selenium tutorial, step-by-step process to set up log4j. How to perform & configured log4j with different two ways. Here we will configure log4j manually using a configuration file for our simple script also we can track or debug our script.
E N D
Tutorial Debugging Selenium Scripts with Logs
Introduction • Here, we have included advanced ideas that would benefit from optimizing the Testing Automation framework and produces more clarity to the users. • We would consider the logging feature and it's possible, debugging capabilities etc. • Benefits of Logging in Selenium Scripts:
Log4j Log4j – A Java-based Logging Utility • Let's Discuss the technical aspects of logging. • Log4j was a decision of collaborative endeavors of people at Secure Electronic Marketplace for Europe to produce a utility. • Log4j is an open source tool and licensed under IBM Public License. • It would support us generating logs and hence the log4j came into consideration in the year 1996. • There are three main elements that form the implementation of log4j:
1. Loggers Logger Class: It is a java based utility/API that has grown all the generic methods already done so that users can enable to use Log4j. • Log Levels: Log levels are commonly identified as to printing methods. These are utilized for printing log messages. • 5 Types of Log Levels: • error() • warn() • info() • debug() • log() • We have to only call any of the above printing methods over the logger instance.
2. Appenders Now where we can get to view the logs? Answer is "Appenders“ Appenders are generally applied to define the data source/medium where the logs should be created.
3. Layouts Layouts are a utility that enables the user to choose the wanted set up in which the logs would be performed. Appenders and Layout have a tight coupling between them. Make a note that the user is leveraged to determine various appenders, each map with a different layout.
Installation • Here we learnt about the basics of log4j and its parts, now let's implement these phenomenafor Debugging Selenium Scripts with Logs • Logging using log4j can be performed and configured in particularly two ways: • Using Script via Programmatically • Configuration files via Manually • Above both suggested configuration ways have merit as well as demerits. • Here we will configure log4j manually using Configuration files as its comfort and simplicity.
Continue To create a log4j.xml file. You can use below code in configuration file:
Continue • The output of the above code is: • 01-07-2014 12:56:32 INFO [GmailLogin]: Sample log message • Here, • First is Execution Date • Second is Time in which the test step was performed • Third is one of the log level • Fourth is name of the test class • Fifth is Log message • Put the log4j.XML file into the project’s root folder/base directory now.
Continue Now parse the log4j.xml file
Continue Now we have to generate a test class “GmailLogin.java” under the project. Then import the logger class to be able to perform the log statements. Syntax: import org.apache.log4j.Logger; Now instantiate the object of the Logger class Syntax: //object initialization for log static Logger log = Logger.getLogger(Demo.class.getName()); This logger will be used over the complete test class to create the log statements.
Conclusion Here we used log4j use to perform logging. We explained the essential parts that form log4j from a usability view. With the Appenders and layouts, you can leverage to determine the wanted logging form/model and the data reference/position. References: Debugging Selenium Scripts with Logs (Log4j Tutorial) Debugging Selenium Scripts with Logs
Contact Us Web: www.nexsoftsys.com Email: info@nexsoftsys.com Call: +1-646-971-0799