Debugging Selenium Scripts with Logs

debugging-selenium-scripts-logs

Whenever you are maintaining automated Selenium tests, it is very important to have a logging framework which makes debugging very easy. You can’t find the failures with console output command in Java. You should have proper logs so that you can easily debug the selenium scripts with ease. Now, you would be imagining why we actually require logging framework. So, a good logging framework will give us a good understanding of tests suite execution. You can store the logs in third-party files that can be shared easily with any technical person.

Also, a good logging framework makes the debugging very easy and failures can be identified very easily. Defect resolution decreases exponentially with logs. Logs tell about the health of the project to the stakeholders. So, you can imagine how important is to maintain a good logging framework.

Log4j - Logging Framework details

For now, we will be using as a Log4j logging framework. This framework is a JAVA-based API which is generally used by developers and testers to generate logs. It is open source so you would get it for free. You can download and use this API for logging purpose.

Now, let us dig more into the components of log4j. Let’s see what they are.

  1. Loggers

    It has all the information about the log level. Logs severity depends on the action being performed. So, different log levels are there which represents different log severity in a project.

    The first step to use logger would be creating an object of Logger class.

    Logger logger = Logger.getLogger("ClassName");

    Let’s look at the severity levels which log4j provides. It has basically 5 kinds of severity log levels. Let’s look into details of these levels.

    • All: You can log almost everything with this type of log level.

    • DEBUG: You can print the debugging information with this log level and most developers will be using this log level.

      log.debug("info”);

    • INFO: Whenever you want to print something related to the progress of an operation or a test, and then you should use this log level.

      log.info("info");

    • WARN: Whenever you find some faulty and unexpected system behaviour, then you should use WARN log level.

    • ERROR: Whenever an error can be traced then you should use an ERROR log level.

    • FATAL: Whenever you find something which can crash the system, then you should use FATAL log level.

    • OFF: It disables the logging.

  2. Appenders

    Now, you have noted what will be the logs but what would happen to those logs that would be decided by appenders. It will be sending log events to the destination folder. There are basically three types of appenders. Let’s have a look at those first.

    1. ConsoleAppender Logs is used for sending logs to standard output.

    2. FileAppender Logs will help in sending logs to the destination folder.

    3. Rolling File Appenders will help in defining log file size. After that size, if files, the new log file will be created and logs will be printed in that.

  3. Layouts

    It has different methods which allow formatting of logs. It formats the logging information in different formats. There are basically two methods which are available to get a logger object to define the layout of the logs.

    Public static Logger getRootLogger()

    Public static Logger getLogger(String name)

  4. Steps to configure Log4j into your existing framework

    We will configure the log4j by having DEBUG level for lofs and RollingFileAppennder for sending logs to the destination folder. Let’s see how root logger will be set up and configured. “logfiles.logs” is the filename on which all logs will be sent. Pattern layout would be selected for configuration purpose for the very first time.

    Root logger ----------- log4j.rootLogger=DEBUG,file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=Downloads://SeleniumLogs log4j.appender.file.maxFileSize=600KB log4j.appender.file.maxBackupIndex=5 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c<strong>{1}</strong>:%L - %m%n log4j.appender.file.Append=false Application Logs ---------------- log4j.logger.devpinoyLogger=DEBUG, dest1 log4j.appender.dest1=org.apache.log4j.RollingFileAppender log4j.appender.dest1.maxFileSize=600KB log4j.appender.dest1.maxBackupIndex=6 log4j.appender.dest1.layout=org.apache.log4j.PatternLayout log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n log4j.appender.dest1.File=downloads: //Manual.logs log4j.appender.dest1.Append=false

    Let’s look at the terms which are used above so that you can have a better understanding of those terms.

    • File: This is actually the file name in which the logs will be printed.  
    • MaxFileSize: So, you have to define the maximum size of the log file so that after size gets completed, a new log file will be created.
    • MaxBackupIndex: Maximum number of files which you want logger to backup.
    • Layout: It does the formatting of the logs.  
    • Append: It can have true or false values. If it is false, a new file is created after a maximum size of the log file has been reached. If it is true, then a new file would not be created.
  5. Steps to use Log4j with Selenium
    • First of all, create a JAVA project with name Log4jFramework

    • You have to import all the log4j jars. First, download them from the internet and then go to configure build path and then go to the libraries folder. Select all external jars and then select all the jar files. You can see the below shot for more details.

    • selenium-scripts
    • Now, what you have to do is to make a file name log4j.properties. You can create two more files one will be manual logs and one will be selenium logs in downloads folder.

      After you are done with that, you have to add the following code in log4j.xml file you have just created:

      <?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>
    • Now, you have to create a java class in which you will actually call the logger instance and log the information. You can make a class name as log4jTest and then put the below coding in the file.

    • import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.apache.log4j.Logger; public class LoggingFramework { public static void main(String[] args) { WebDriver driver = new ChromeDriver(); Logger logger = Logger.getLogger(Log.class.getName()); driver.get("url of the application"); logger.debug("opening website"); // it stores these logs in manual.log file. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); logger.debug("weight entered"); driver.findElement(By.name("wg")).sendKeys("100"); logger.debug("kilograms selected"); driver.findElement(By.name("opt1")).sendKeys("kilograms"); logger.debug("feet height"); driver.findElement(By.name("opt2")).sendKeys("20"); logger.debug("inch height"); driver.findElement(By.name("opt3")).sendKeys("5"); logger.debug("calculating"); driver.findElement(By.name("cc")).click(); logger.debug("SIUnit value"); String unitSI = driver.findElement(By.name("si")).getAttribute("value "); logger.debug("USUnit value"); String unitUS = driver.findElement(By.name("us")).getAttribute("value"); logger.debug("UKUnit value"); String unitUK = driver.findElement(By.name("uk")).getAttribute("value"); logger.debug("overall description"); String msg = driver.findElement(By.name("desc")).getAttribute("value "); System.out.println("SIUnit = " + unitSI); System.out.println("USUnit = " + unitUS); System.out.println("UKUnit = " + unitUK); System.out.println("note = " + msg); driver.quit(); } }
    • After you will run your tests, you will get the logs as below shot.

      selenium-scripts-log

How to Debug with Logs

After you are done with tests execution, you can go through the logs which are generated. If at later point of times, some critical bug comes in and you have to look what went wrong and you want to go through the logs at the previous point of time, you can then go through the logs to find the root cause of the bug.

After the tests are run, you can quickly scan if there is ERROR or FATAL logs available. In this way, you will quickly resolve those severe issues. Also, logs are a common medium which can be shared with all stakeholders for more clarity.

Conclusion

So, with the help of log4j, you will be able to generate logs and hence, you will be able to debug the selenium scripts. So if you are providing Selenium Test Automation Services, then you can now easily debug the selenium scripts with the log4j logging framework. All the best!!

Some of our clients

team