- During the running of test case user wants some information to be logged in to the console.
- Information could be any detail depends upon the purpose.
- Keeping this in mind that we are using Selenium for testing, we need the information which helps the user to understand the test steps or any failure during the test case execution.
- Using Log4j it is possible to enable loggings during the Selenium test case execution so any failure in execution will be reported in the system.
- Log4j is a fast, flexible and reliable logging framework (APIS) written in Java developed in early 1996.
- It is distributed under the Apache Software License.
- It is a tool used for small to large scale Selenium Automation projects.
- Log4j is a tool to help the programmer output log statements to a variety of output targets.
- It is an open source.
- It is possible to store the flow details of our Selenium Automation in a file or databases.
- Helpful to know the status of a project while it is executing.
- In case of problems with an application, it is helpful to enable logging so that the problem can be located.
- loggers: Responsible for capturing logging information.
- appenders: Responsible for publishing logging information to various preferred destinations.
- layouts: Responsible to format logging information in different styles.
- ALL: All levels including custom levels.
- DEBUG: Designates fine-grained informational events that are most useful to debug an application.
- INFO: Designates informational messages that highlight the progress of the application at coarse-grained level.
- WARN: Designates potentially harmful situations.
- ERROR: Designates error events that might still allow the application to continue running.
- FATAL: Designates very severe error events that will presumably lead the application to abort.
- OFF: The highest possible rank and is intended to turn off logging.
How to download log4j?
- 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.
- 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.jar file in it.
- Right click on your project folder and go to Build Path -> Configure Build path. It will open java build path window.
- Click on Add External JARs button and select log4j.jar file. It will add log4j.jar file in your build path.
- Click on OK button to close java build path window.
Create log4j.properties file
- Create log4j.properties file in your project folder.
- Copy following code in it.
- Set run configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # Root logger option log4j.rootLogger=DEBUG, file # Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=C:\\MyProject\\logs\\SeleniumPomFramework.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n |
Here we will are using DEBUG level root logger and RollingFileAppender and layout will be PatternLayout.
Create a class to perform logging:
To log a message, first, create a final static logger and define a name for the logger, normally, we use the full package class name.
final static Logger Log = Logger.getLogger(Log4jDemo.class.getName());
Please see the following example.
final static Logger Log = Logger.getLogger(Log4jDemo.class.getName());
Please see the following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Log4jDemo { private static WebDriver driver; final static Logger Log = Logger.getLogger(Log4jDemo.class.getName()); public static void main(String[] args) { // Allows the configuration of log4j from an external file. PropertyConfigurator.configure("../MyProject/log4j.properties"); // Create a new instance of the Firefox driver Log.info("Instantiating chrome driver"); System.setProperty("webdriver.chrome.driver", "C:\\Vinod\\Java\\Workspace\\TestEQ\\drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); Log.info("Implicit wait applied on the driver for 10 seconds"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); Log.info("Launching Selenium blogspot"); driver.get("http://www.onlinestore.toolsqa.wpengine.com"); Log.info("Closing the browser"); driver.quit(); } } |
No comments:
Post a Comment