- If we are executing Test Suite with hundreds of automated test cases then logging all the events might be useful.
- Helps in debugging, in case of test automation failures
- gives a fair understanding of application run
- log output can be saved into some external medium and can be studied later on
Loggers- Instance of Logger class.
-used for printing the log messages. Levels are of following kinds:
error()
warn()
info()
debug()
log()
Appenders- might be a console, a log file, printer, etc.
Layouts- is the formatting of the log output.
Steps:
Step1. The package distribution is widely available. Thus, download the latest version of log4j jar from here.
Step2. In eclipse, configure build path and add log4j.jar as an external jar.
Step3 Create an log4j xml file.
<?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{yyyy-MM-dd HH:mm:ss} [%c] (%t:%x) %m%n" />
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>
Step 4:Java class
package Logs;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Loggssample
{
//Initialization of Logger Instance.
static final Logger logger = LogManager.getLogger(Loggssample.class.getName());
public static void main(String[] args) throws IOException, AWTException
{
///“DOMConfigurator” to parse the configuration file “log4j.xml” and setup logging.
DOMConfigurator.configure("log4j.xml");
logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");
logger.info("TEST Has Been Started");
WebDriver driver = new FirefoxDriver();
//Puts a Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch website
driver.get("https://www.gmail.com");
logger.info("Open gmail application");
//Maximize the browser
driver.manage().window().maximize();
// Enter the username
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("username");
logger.info("Enter the user's username");
// Enter the password
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("Passwd")).sendKeys("password");
logger.info("Enter the user-password");
// Press enter to sign in
Robot robot = new Robot();
robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);
logger.info("Enter sign in");
logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");
//Close the Browser.
driver.close();
}
}
======================================================================
2014-09-16 15:30:18,931 INFO [log4jXmlTest] # # # # # # # # # # # # # # # # # # # # # # # # # # #
2014-09-16 15:30:24,773 INFO [log4jXmlTest] TEST Has Been Started
2014-09-16 15:30:24,773 INFO [log4jXmlTest] Open gmail application
2014-09-16 15:30:26,850 INFO [log4jXmlTest] Enter the user's username
2014-09-16 15:30:27,460 INFO [log4jXmlTest] Enter the user-password
2014-09-16 15:30:27,491 INFO [log4jXmlTest] Enter sign in
To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere

No comments:
Post a Comment