我试图从我的路径到范围报告附加失败测试用例的屏幕截图,但不知何故,我无法附加到它。
我尝试了可能的解决方案,但失败了。我使用了扩展区报告版本3
下面是在独立extendreport类中挖掘完整代码:
package com.qa.ExtentReportListener;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.testng.IReporter;
import org.testng.IResultMap;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.xml.XmlSuite;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
import com.crm.qa.util.TestUtil;
public class ExtentTestNGIReporterListener implements IReporter {
private static final String OUTPUT_FOLDER = "test-output/";
private static final String FILE_NAME = "Extent.html";
private ExtentReports extent;
private ExtentTest test;
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
init();
for (ISuite suite : suites) {
Map<String, ISuiteResult> result = suite.getResults();
for (ISuiteResult r : result.values()) {
ITestContext context = r.getTestContext();
buildTestNodes(context.getFailedTests(), Status.FAIL);
buildTestNodes(context.getSkippedTests(), Status.SKIP);
buildTestNodes(context.getPassedTests(), Status.PASS);
}
}
for (String s : Reporter.getOutput()) {
extent.setTestRunnerOutput(s);
}
extent.flush();
}
private void init() {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
htmlReporter.config().setDocumentTitle("ExtentReports - Created by TestNG Listener");
htmlReporter.config().setReportName("ExtentReports - Created by TestNG Listener");
htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
htmlReporter.config().setTheme(Theme.STANDARD);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setReportUsesManualConfiguration(true);
}
private void buildTestNodes(IResultMap tests, Status status) {
if (tests.size() > 0) {
for (ITestResult result : tests.getAllResults()) {
test = extent.createTest(result.getMethod().getMethodName());
for (String group : result.getMethod().getGroups())
test.assignCategory(group);
if (result.getThrowable() != null) {
test.log(status, result.getThrowable());
}
else {
test.log(status, "Test " + status.toString().toLowerCase() + "ed");
}
test.getModel().setStartTime(getTime(result.getStartMillis()));
test.getModel().setEndTime(getTime(result.getEndMillis()));
}
}
}
public void down(ITestResult result) throws IOException{
if(result.getStatus()==ITestResult.FAILURE){
test.log(Status.FAIL, "TEST CASE FAILED IS "+result.getName()); //to add name in extent report
test.log(Status.FAIL, "TEST CASE FAILED IS "+result.getThrowable()); //to add error/exception in extent report
String screenshotPath = TestUtil.takeScreenshotAtEndOfTest();
test.fail("Test Case failed check screenshot below"+test.addScreenCaptureFromPath(screenshotPath));
//extentTest.log(Status.FAIL, MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath).build()); //to add screenshot in extent report
//extentTest.fail("details").addScreenCaptureFromPath(screenshotPath);
}
else if(result.getStatus()==ITestResult.SKIP){
test.log(Status.SKIP, "Test Case SKIPPED IS " + result.getName());
}
else if(result.getStatus()==ITestResult.SUCCESS){
test.log(Status.PASS, "Test Case PASSED IS " + result.getName());
}
extent.flush();
}
private Date getTime(long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
}
下面是我包含屏幕截图方法的util类:
public static String takeScreenshotAtEndOfTest() throws IOException {
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String destination = System.getProperty("user.dir") + "/screenshots/" + dateName
+ ".png";
File finalDestination = new File(destination);
FileHandler.copy(source, finalDestination);
return destination;
}
6条答案
按热度按时间ggazkfy81#
如果您想对带有测试类名称的失败测试用例进行截屏,请使用以下代码段。
hfyxw5xn2#
范围报告已提供用于获取屏幕快照的实用程序。请参阅以下链接:
https://extentreports.com/docs/versions/3/java/#automatic-screenshot-management
还可以找到快照和相同的代码:
how to take Screenshot using extent report
htrmnn0y3#
请尝试使用以下方法:
UPDATE:如果这样做不起作用,就用markup来转换内部记录器。
vuv7lop34#
fjaof16o5#
在after方法中使用此代码
并在buildtestnodes中添加以下代码
nnt7mjpx6#
下面这个例子是用Java