gpt4 book ai didi

java - 如何使用 TestNG 在 Selenium 中的 IReporter 界面中使用 screencapture?

转载 作者:行者123 更新时间:2023-12-02 04:53:35 33 4
gpt4 key购买 nike

我在Selenium中使用IReporter TestNG接口(interface),但是如何捕获屏幕截图并添加到失败测试用例的范围报告中?

请帮我找到解决方案。

最佳答案

下面是将失败的测试用例屏幕截图附加到范围报告的代码。

  • MyReporterClass 实现 IReporter 接口(interface):它迭代测试套件中的测试用例并保存每个测试用例的状态。
public class MyReporterClass implements IReporter {

@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
//Iterating over each suite included in the test
for (ISuite suite : suites) {
//Following code gets the suite name
String suiteName = suite.getName();
//Getting the results for the said suite
Map<String, ISuiteResult> suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
ITestContext tc = sr.getTestContext();
System.out.println("Passed tests for suite '" + suiteName +
"' is:" + tc.getPassedTests().getAllResults().size());
System.out.println("Failed tests for suite '" + suiteName +
"' is:" + tc.getFailedTests().getAllResults().size());
System.out.println("Skipped tests for suite '" + suiteName +
"' is:" + tc.getSkippedTests().getAllResults().size());
}
}
}
}
  • getScreenshot() 方法:捕获屏幕截图并返回屏幕截图的目标路径。
public class ExtentReportsClass{
public static String getScreenshot(WebDriver driver, String screenshotName) throws Exception {
//below line is just to append the date format with the screenshot name to avoid duplicate names
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
//after execution, you could see a folder "FailedTestsScreenshots" under src folder
String destination = System.getProperty("user.dir") + "/FailedTestsScreenshots/"+screenshotName+dateName+".png";
File finalDestination = new File(destination);
FileUtils.copyFile(source, finalDestination);
//Returns the captured file path
return destination;
}
}
  • @AfterMethodpublic void getResult(ItestResult result):每次测试用例执行后执行,并将失败的测试用例截图附加到Extent报告中。
@AfterMethod
public void getResult(ITestResult result) throws IOException{
if(result.getStatus() == ITestResult.FAILURE){
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
//To capture screenshot path and store the path of the screenshot in the string "screenshotPath"
String screenshotPath = ExtentReportsClass.getScreenshot(driver, result.getName());
//To add it in the extent report
logger.log(LogStatus.FAIL, logger.addScreenCapture(screenshotPath));
}else if(result.getStatus() == ITestResult.SKIP){
logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
}
  • testng.xml 文件:在 xml 文件中包含以下监听器标记。
<listeners>
<listener class-name="packagename.MyReporterClass" />
</listeners>

关于java - 如何使用 TestNG 在 Selenium 中的 IReporter 界面中使用 screencapture?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56420765/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com