Selenium
Cross Browser Testing using Selenium WebDriver
What is Cross Browser Testing? Cross Browser Testing is a type of functional test to check that...
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. Log4J has been ported to the C, C++, C#, Perl, Python, Ruby and Eiffel Languages. It is a tool used for small to large scale Selenium Automation projects.
Note: In log4j properties we can call appender with any name. There are other appenders as well but we will restrict to these few.
The Logger class provides different methods to handle logging activities. It provides two static methods for obtaining a Logger Object.
Public static Logger getRootLogger() Public static Logger getLogger(String name)
To configure log4j we have to decide which appender to implement. Accordingly, parameters of appender will be set.
#Root logger
log4j.rootLogger=DEBUG,file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\gtupapers\\src\\Selenium.logs
log4j.appender.file.maxFileSize=900KB
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=900KB
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=D:\\ gtupapers\\src\\Manual.logs
log4j.appender.dest1.Append=false
In the above Example, we have configured log4j to log in two different files named as Selenium.log and Manual.log.
In code, we have used "log" as a reference variable referencing getLogger method of Logger Class
Logger log = Logger.getLogger("devpinoyLogger");Use "log" referencing variable and debug method to log the information we want.
log.debug("--information--");Step 1) In Eclipse create a new project with name log4j_demo
Step 2) Right click on src -> Build Path -> Configure Build Path
Step 2) Click on Libraries and Add Log4J Library . You can download it from https://logging.apache.org/log4j/1.2/download.html
Step 3) Create a new file. This file will include all the log4j configuration
Create two more files and give them names such as Selenium.logs and Manual.logs. These files will contain all the logs created by system and manually logged statements
Step 4) In log4j.properties copy the entire configuration.
Step 5) Create main class:
Step 6) Copy the following code in to the main class
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.apache.log4j.Logger;
public class LoggingDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
Logger log = Logger.getLogger("devpinoyLogger");
driver.get("http://healthunify.com/bmicalculator/");
log.debug("opening webiste");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
log.debug("entring weight");
driver.findElement(By.name("wg")).sendKeys("87");
log.debug("selecting kilograms");
driver.findElement(By.name("opt1")).sendKeys("kilograms");
log.debug("selecting height in feet");
driver.findElement(By.name("opt2")).sendKeys("5");
log.debug("selecting height in inchs");
driver.findElement(By.name("opt3")).sendKeys("10");
log.debug("Clicking on calculate");
driver.findElement(By.name("cc")).click();
log.debug("Getting SIUnit value");
String SIUnit = driver.findElement(By.name("si")).getAttribute("value");
log.debug("Getting USUnit value");
String USUnit = driver.findElement(By.name("us")).getAttribute("value");
log.debug("Getting UKUnit value");
String UKUnit = driver.findElement(By.name("uk")).getAttribute("value");
log.debug("Getting overall description");
String note = driver.findElement(By.name("desc")).getAttribute("value");
System.out.println("SIUnit = " + SIUnit);
System.out.println("USUnit = " + USUnit);
System.out.println("UKUnit = " + UKUnit);
System.out.println("note = " + note);
driver.quit();
}
}
In the above code, we visit http://healthunify.com/bmicalculator/and verify BMI calculator. The weight entered is 87KG and the height is 5 Feet 10 inches. The script checks output in SE, US and UK units.
Using Logger.getLogger("devpinoyLogger") we create system level logs
Using log.debug method we store data into Manual.log
Step 7) Run the script. Open the location of Manual and Selenium logs to check logging data.
Selecting follow tail option enables tailing of logs which means LogExpert automatically updates the log file when script is in execution phase. If we use any other editor like notepad then we have to close and reopen the file again and again to update the logs. But with ExpertTool in Follow Tail Mode this is not required.
Following images shows the layout of the logs
Using LogExpert tool, one can debug logs created by the selenium webdriver as in this tool once can
This tool also helps to partition the data into different columns.
What is Cross Browser Testing? Cross Browser Testing is a type of functional test to check that...
In this tutorial, you will learn how to integrate Cucumber with Selenium Webdriver. What is...
Training Summary Selenium is a popular open-source web-based automation tool. This online course is...
What is Page Object Model? Page Object Model (POM) is a design pattern, popularly used in test...
Forms are the fundamental web elements to receive information from the website visitors. Web forms have...
In Selenium, "Waits" play an important role in executing tests. In this tutorial, you will learn...