cucumber - 在 Cucumber 框架中作为 TestNG 运行时,ExtentReports 将所有测试用例的测试名称显示为“runScenario”
问题描述
我已将现有的 Selenium-TestNG 框架转换为 Cucumber,并且正在使用 ExtentReports 进行报告。我在 Listeners 类中截屏。当我将测试作为 TestNG 测试运行时,我的所有截图都将被覆盖,并且范围报告将所有测试用例的测试名称显示为“runScenario”:
我想为范围报告中的所有测试场景显示功能文件场景名称,而不是“ runScenario ”,并防止覆盖屏幕截图。
以下是来自各个文件的代码:
功能文件(Logout.feature):
Feature: Logout feature
Background:
Given User is on Salesforce login page
@SmokeTest @Low @PositiveTest @Logout
Scenario: Logout of application1
Given "RMUser" has logged into Salesforce application
When User clicks on logout button
Then Users is logged-out from the application
@SmokeTest @Low @PositiveTest @Logout
Scenario: Logout of application2
Given "RMUser" has logged into Salesforce application
When User clicks on logout button
Then Users is logged-out from the application
跑步者档案:
package cucumberOptions;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(
features = "src/test/java/featuresCommon",
glue = "stepDefinitions",
tags="@Logout",
//tags="@Login and @SanityTest",
//tags="@SanityTest or @SmokeTest",
//tags="not @SanityTest",
dryRun = false,
monochrome = true,
plugin = {
"pretty",
"json:target/cucumber-reports/Cucumber.json",
"html:target/cucumber-reports/Cucumber.html",
"junit:target/cucumber-reports/Cukes.xml"
}
)
public class TestNGRunner extends AbstractTestNGCucumberTests{
}
监听类:
public class Listeners extends Base implements ITestListener {
ExtentReports extentReport = ExtentReporterNG.getExtentReportObj();
ExtentTest test;
public void onTestStart(ITestResult result)
{
test = extentReport.createTest(result.getMethod().getMethodName());
}
public void onTestSuccess(ITestResult result)
{
test.log(Status.PASS, "Test case Passed");
try
{
String path = takeScreenshot(driver, "Pass/" + result.getInstanceName() + "/" + result.getMethod().getMethodName());
test.addScreenCaptureFromPath(path, result.getMethod().getMethodName());
}
catch (Exception e)
{
log.fatal("Exception in onTestSuccess Method of Listeners class");
log.fatal(e.getMessage() +" : " + e.getStackTrace());
System.out.println("Exception " + e.getMessage());
System.out.println("Exception " + e.getStackTrace());
}
driver.quit();
}
public void onTestFailure(ITestResult result) {
test.log(Status.FAIL, "Test case Failed");
test.fail(result.getThrowable());
try
{
String path = takeScreenshot(driver, "Fail/" + result.getInstanceName() + "/" + result.getMethod().getMethodName());
test.addScreenCaptureFromPath(path, result.getMethod().getMethodName());
}
catch(Exception e)
{
log.fatal("Exception in onTestFailure Method of Listeners class");
log.fatal(e.getMessage() +" : " + e.getStackTrace());
System.out.println("Exception " + e.getMessage());
System.out.println("Exception " + e.getStackTrace());
}
driver.quit();
}
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
extentReport.flush();
}
基本文件(截图的相关截图方法):
public static String takeScreenshot(WebDriver driver, String testcaseName) throws IOException {
String destinationPath = System.getProperty("user.dir") + "/screenshots/" + testcaseName + ".png";
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File(destinationPath));
return destinationPath;
}
解决方案
推荐阅读
- c++ - 转换结构数据的字节顺序
- django - Elastic Beanstalk 上的 Django,如何将已编辑视频的输出文件保存到 S3 存储桶中
- java - SwaggerGen 枚举生成空数据类型
- centos7 - 如何在 centos 7 上运行 .sln 文件
- java - 如何获取找到该单词的行号?
- python - 为什么同一类的不同对象显示相似的结果?出路是什么?
- javascript - React Native:如何将屏幕更改/导航为子组件?
- azure - 由于 netcore 3.1.4 安全补丁导致 Azure 出错
- sling - 如何在 Apache Sling 或 Jackrabbit Oak 中进行用户模拟?
- linux - 如何使用并行命令调整多个 png 文件的大小并保留源文件中的部分名称?