Log4J Logging

During the running of test case user wants some information to be logged in 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. With the help of Log4j it is possible to enable loggings during the Selenium test case execution for e.g. let’s say you have encountered a failure in automation test script and it has to be reported in the system. The set of information that you have required to report a bug is :

  • A complete test steps to replicate the scenario
  • Issue, Description of the failure or reason for the failed test case
  • Time stamp for the developers to investigate the issue in detail

Log4j helps us to acheive the above objectives in Selenium Webdriver. When logging is wisely used, it can prove to be an essential tool.

Logging inside the Methods

Logging inside the testcase is very tedious task and sooner or later you will find it boring and annoying. Plus everybody has their own way of writing log messages and messages can be less informative and confusing. So why not make it universal. Writing logs message inside the methods is much helpful way, with that we can avoid lots of confusions, save lot of time and maintain consistency.

How to do it…

1) Download JAR files of Log4j and Add Jars to your project library. You can download it from here. That’s all about configuration of Apache POI with eclipse. Now you are ready to write your test.

2) Create a new XML file – log4j.xml and place it under the Project root folder.

3) Paste the following code in the log4j.xml file.

Note: After pasting the code make sure that the code is exactly same, as copying from HTML may change some symbols (“) to (?).

Let’s take an example of our previous Apache_POI_TC test case and put log messages in every method and module it interacts with.

4) To achieve that we need to create a static Log class, so that we can access its log method in any of our project class. Log class will look like this:

5) Insert log messages in Home_Page class of pageObject package.

6) Insert log messages in LogIn_Page class of pageObject package.

7) Insert log messages in SignIn_Action class of appModule package.

8) Now it is the time to insert log message in to your test script but before that create a ‘New Class‘ and name it as Log4j_Logging_TC by right click on the ‘automationFramework‘ Package and select New > Class.

Once your test is complete, go to your project root folder and open the log file. Your log file will look like this:

Log4j-Result

Your Project explorer window will look like this now.

Log4j-Window

Note: Still you see some logging in the main test script which ideally has to be avoided or minimised. I have done this so that it can make sense to you. In coming chapters you will see how to avoid logging in main test script.

Next chapter is all about TestNG framework which is the most important chapter of the Selenium Automation Frameworks.  Before that I request you to visit first Test Case with TestNG.

 

navigate-left-icon

navigate-right-icon