首页 > 解决方案 > 在 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;
    }

标签: cucumberextentreports

解决方案


推荐阅读