As I have explained that how we can get the data from property files and the same process is applied here as well but the way is little bit different. To achieve this follow the below steps:
- Create a blank file under project like "AppLogs.txt"
- Create a log4j.properties file and place the below code in it which defines the display format of the log file.
log4j.rootLogger=INFO,Appender
log4j.appender.Appender=org.apache.log4j.FileAppender
log4j.appender.Appender.File=./Application Logs/AppLogs.txt
log4j.appender.Appender.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender.layout.conversionPattern=%-7p %d [%t] %C %X -%m%n
log4j.appender.File.maxFileSize=500KB
log4j.appender.File.MaxBackupIndex=3
log4j.appender.Appender.Append=false
log4j.appender.Appender=org.apache.log4j.FileAppender
log4j.appender.Appender.File=./Application Logs/AppLogs.txt
log4j.appender.Appender.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender.layout.conversionPattern=%-7p %d [%t] %C %X -%m%n
log4j.appender.File.maxFileSize=500KB
log4j.appender.File.MaxBackupIndex=3
log4j.appender.Appender.Append=false
- Download log4j Jar file https://mvnrepository.com/artifact/log4j/log4j/1.2.17
- Import this jar file in your project "import org.apache.log4j.Logger;"
- Now create an object of Logger class and instantiate this like below
- Pass the reference of the log4j.properties file where the format of execution logs are given.
Below code will show you how to write the logs.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.testng.annotations.Test;
public class NewTest
{
public Properties configProp;
public FileInputStream file;
public Logger log=Logger.getLogger(NewTest.class.getName());
@Test
public void FetchData() throws IOException
{
PropertyConfigurator.configure("./PropertyFIle/log4j.properties");
file=new FileInputStream("./PropertyFIle/Config.properties");
configProp=new Properties();
configProp.load(file);
System.out.println("Value of Browser key is :"+ configProp.getProperty("Browser"));
log.info("Chrome browser launched");
System.out.println("Value of URL key is :"+ configProp.getProperty("URL"));
log.info("URL opened");
System.out.println("Value of Implicitwait key is :"+ configProp.getProperty("ImplicitWaitTime"));
log.info("Implicitwait time implemented throughout the script");
}
}
Output
[RemoteTestNG] detected TestNG version 7.0.0
Value of Browser key is :Chrome
Value of URL key is :https://www.goggle.co.in
Value of Implicitwait key is :20
PASSED: FetchData
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Output of log file which is AppLogs.txt
[RemoteTestNG] detected TestNG version 7.0.0
Value of Browser key is :Chrome
Value of URL key is :https://www.goggle.co.in
Value of Implicitwait key is :20
PASSED: FetchData
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Output of log file which is AppLogs.txt
No comments:
Post a Comment