Please find below the complete code for testNGlistner
. Kindly check.
package pom;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import generic.BaseTest;
public class testNgListners extends BaseTest implements ITestListener {
ExtentHtmlReporter htmlReporter;
ExtentReports extent;
ExtentTest logger;
@Override
public void onTestStart(ITestResult result) {
}
@Override
public void onTestSuccess(ITestResult result) {
logger = extent.createTest(result.getName());
logger.log(Status.PASS, MarkupHelper.createLabel(result.getName(), ExtentColor.GREEN));
}
@Override
public void onTestFailure(ITestResult result) {
System.out.println("hii");
logger = extent.createTest(result.getName());
logger.log(Status.FAIL, MarkupHelper.createLabel(result.getName(), ExtentColor.RED));
if (result.getStatus() == ITestResult.FAILURE) {
TakesScreenshot take = (TakesScreenshot) driver;
File srcFile = take.getScreenshotAs(OutputType.FILE);
File destFile = new File("./test-output/Sceenshots/" + result.getName() + ".png");
try {
FileUtils.copyFile(srcFile, destFile);
System.out.println("Screenshot is been taken for failed test case: " + result.getName());
logger.fail("Screenshot below" + logger.addScreenCaptureFromPath("./test-output/Sceenshots/" + result.getName() + ".png"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onTestSkipped(ITestResult result) {
//logger=extent.createTest(result.getName());
logger.log(Status.SKIP, MarkupHelper.createLabel(result.getName(), ExtentColor.ORANGE));
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {}
@Override
public void onStart(ITestContext context) {
htmlReporter = new ExtentHtmlReporter("./test-output/extent.html");
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Host name", "localhost");
extent.setSystemInfo("Environment", "QA");
extent.setSystemInfo("user", "Arun K M");
htmlReporter.config().setDocumentTitle("Hybrid Automation Report");
htmlReporter.config().setReportName("Functional Testing");
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
}
@Override
public void onFinish(ITestContext context) {
extent.flush();
}
}
while taking a screenshot it throws null pointer exception. Kindly help me to resolve this. PFB the error details:
java.lang.NullPointerException at pom.testNgListners.onTestFailure(testNgListners.java:53) at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:67) at org.testng.internal.Invoker.runTestListeners(Invoker.java:1389) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1042) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:283) at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75) at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:120) at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345) at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
Hi Krishna,
PFB the basetest code.
package generic;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class BaseTest implements Autoconst {
File file=new File("\\\\192.168.70.39\\IT Share\\Automation\\ERP Automation\\credential.properties");
Properties prop = new Properties();
public WebDriver driver;
@Parameters({"browser"})
@BeforeMethod
public void precondition()
{
if(browser.equals("chrome"))
{
FileInputStream fileInput =null;
try {
fileInput = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
prop.load(fileInput);
} catch (Exception e) {
e.printStackTrace();
}
System.setProperty(GECKO_KEY,GECKO_VALUE);
ChromeOptions options = new ChromeOptions();
options.setPageLoadStrategy(PageLoadStrategy.NONE);
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get(prop.getProperty("URL"));
driver.manage().window().maximize();
}
}
@AfterMethod
public void postcondition()
{
driver.quit();
}
}
extent
object is being instantiated. – Krishnan Mahadevan