Пожалуйста, помогите решить указанную ниже проблему.
Объект создан ExtentTest в другом классе. При запуске файла XML - код выдаёт ошибку исключения Null Pointer для строки er.logger = er.extent.createTest ("Test1"); ..................... в приведенном ниже тестовом примере Класс
Невозможно понять, как решить проблему.
Пожалуйста, помогите
Below is the Extent Report Listener Class
package UtilsClasses;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
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 BaseandTestClasses.BaseConnection;
public class ExtentReport extends TestListenerAdapter
{
public ExtentHtmlReporter htmlReporter;
public ExtentReports extent;
public ExtentTest logger;
public void onTestSkipped(ITestResult tr) //@TestMethodSkipped
{
logger=extent.createTest(tr.getName());
logger.log(Status.SKIP, MarkupHelper.createLabel(tr.getName(), ExtentColor.ORANGE));
}
public void onFinish (ITestContext testContext) //onFinish is Test tag finish in XML
{
extent.flush();
}
public void onStart(ITestContext testContext) //Test tag start in XML
{
String timeStamp= new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
String repName="Test-Report-"+timeStamp+".html";
htmlReporter=new ExtentHtmlReporter(System.getProperty("user.dir")+ "/test-output/"+repName);
htmlReporter.loadXMLConfig(System.getProperty("user.dir")+ "/extent-config.xml");
extent= new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Host name","localhost");
extent.setSystemInfo("Environment","QA");
extent.setSystemInfo("user","kedar");
htmlReporter.config().setDocumentTitle("Gtpl Bank");
htmlReporter.config().setReportName("Automation Test Report");
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.DARK);
}
public void onTestFailure(ITestResult tr) //@TestMethodFailed
{
logger=extent.createTest(tr.getName());
logger.log(Status.FAIL, MarkupHelper.createLabel(tr.getName(), ExtentColor.RED));
//Below is code for Screen-Capture to be done from here, with timestamp
String timeStamp= new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
TakesScreenshot ts = (TakesScreenshot) BaseConnection.driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String screenshotPath = System.getProperty("user.dir")+"/00Screenshots/"+tr.getName()+" - "+timeStamp+".png";
File target = new File(screenshotPath);
try {
FileUtils.copyFile(source, target);
} catch (IOException e1) {
e1.printStackTrace();
}
File f = new File(screenshotPath);
if(f.exists())
{
try {
logger.fail("screenshot is below: "+logger.addScreenCaptureFromPath(screenshotPath));
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public void onTestSuccess(ITestResult tr) //@TestMethodPassed
{
logger=extent.createTest(tr.getName());
logger.log(Status.PASS, MarkupHelper.createLabel(tr.getName(), ExtentColor.GREEN));
}
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
// TODO Auto-generated method stub
}
public void onTestStart(ITestResult tr) {
// TODO Auto-generated method stub
}
}
_________________________________________________________________________________________________________
Below is the Test Case Class
package BaseandTestClasses;
import java.io.IOException;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import PageObjectClasses.PageObjectLoginLogout;
import UtilsClasses.ExtentReport;
public class TC1_LoginLogout1 extends BaseConnection
{
ExtentReport er = new ExtentReport ();
@Test
public void TC1LoginLogout1() throws IOException, InterruptedException
{
PageObjectLoginLogout pgll = new PageObjectLoginLogout(driver);
er.logger=er.extent.createTest("Test1");
System.out.println("In Start");
System.out.println("Started Test");
pgll.setUserName(UName);
System.out.println("Entered valid UserName: "+UName);
logj.info("Entered valid UserName: "+UName);
er.logger.pass("Entered valid UserName: "+UName);
pgll.setPassword(PWord);
System.out.println("Entered valid Password: "+PWord);
logj.info("Entered valid Password: "+PWord);
er.logger.pass("Entered valid Password: "+PWord);
pgll.clickSubmit();
Thread.sleep(10000);
if(driver.getTitle().contains("GTPL Bank Manager"))
{
Assert.assertTrue(true);
System.out.println("Login successful. Test Case Passed");
logj.info("Login successful. Test Case Passed");
er.logger.pass("Login successful. Test Case Passed");
}
else
{
System.out.println("Login failed. Test Case Failed");
logj.info("Login failed. Test Case Failed");
er.logger.fail("Login successful. Test Case Passed");
Assert.assertTrue(false);
}
er.logger.info("Test Case Completed");
}
}
_________________________________________________________________________________________________________
Below is the XML File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="UtilsClasses.ExtentReport"/>
</listeners>
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="BaseandTestClasses.TC1_LoginLogout1" />
<!--<class name="BaseandTestClasses.TC1_LoginLogout2" />
<class name="BaseandTestClasses.TC2_AddCustomer" />-->
</classes>
</test>
</suite>
_________________________________________________________________________________________________________