我正在寻找一种在基本设置之外使用测试步骤和结果数据来实现Advanced ExtentReports的方法。

我目前有一个有效的ExtentReports报告,正在寻找一种使用ExtentReports(Logger)类实施和显示测试步骤的方法。我尝试将(Logger.log)语句添加到类中,但是无法查看ExtentReports文件中的测试步骤。

public static WebDriver driver;
public static ExtentReports extentReports;
public static ExtentTest extentTest;
public static ExtentHtmlReporter htmlReporter;
public static ExtentTest test;
public static ExtentTest logger;

@BeforeSuite
public void beforeSuite(){
         htmlReporter = new ExtentHtmlReporter(new File(("reports/Test Automation Report.html")));
         htmlReporter.loadXMLConfig(new File("extent-config.xml"));
         extentReports=new ExtentReports();
         extentReports.attachReporter(htmlReporter);
         extentReports.setSystemInfo("Environment", "QA");
         htmlReporter.config().setChartVisibilityOnOpen(true);
         htmlReporter.config().setDocumentTitle("AutomationTesting.in Demo Report");
         htmlReporter.config().setReportName("My Own Report");
         htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
         htmlReporter.config().setTheme(Theme.DARK);
    }

    @BeforeMethod
    public void before(Method method) {
        extentTest=extentReports.createTest(method.getName().toString());
        driver = DriverType.chromeDriver();
        driver.manage().timeouts().setScriptTimeout(10,TimeUnit.SECONDS);
        IRISBase base = new IRISBase(driver);
        base.fmaLoad();
    }

    @AfterMethod
    public void getResult(ITestResult result) throws Exception {
        if (driver!=null){
            System.out.println("deleteAllCookies");
            driver.manage().deleteAllCookies();
            }

        if (result.getStatus() == ITestResult.FAILURE)
        {
            System.out.println(">>>Test Failed"); //Which Test Failed and Why (Method Name Output)
            extentTest.log(Status.FAIL,  MarkupHelper.createLabel(result.getName() + " Test case FAILED due to below issues:", ExtentColor.RED));
            extentTest.fail(result.getThrowable());
            System.out.println("Automation Test Run: " + result.getMethod().toString()); //Testing and Results (Relevant)
        }
        else if (result.getStatus() == ITestResult.SUCCESS)
        {
            System.out.println(">>>Test Passed");
            extentTest.log(Status.PASS, MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
            logger.info(MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
            System.out.println("=====================================================================================================");
            System.out.println("Automation Test Run: " + result.getMethod().toString()); //Testing and Results
            System.out.println("=====================================================================================================");
        }
        else if (result.getStatus() == ITestResult.SKIP)
        {
            System.out.println(">>>Test Skipped");
            extentTest.log(Status.SKIP, MarkupHelper.createLabel(result.getName() + " Test Case SKIPPED", ExtentColor.BLUE));
            System.out.println("Automation Test Run: " + result.getMethod().toString()); //Testing and Results
        }

        if(driver!=null) {
            driver.manage().deleteAllCookies();
        }
    }

    @AfterTest
    public void testComplete() {
        extentReports.flush();
        driver.manage().deleteAllCookies();
        driver.quit();
    }

    private static File fileWithDirectoryAssurance(String directory, String filename) {

        Date date = new Date() ;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;

        File dir = new File(directory);
        if (!dir.exists()) dir.mkdirs();
        return new File(directory + "/" + filename);
    }
}


预期的ExtentReports文件
java - 如何为Selenium WebDriver实现Advanced ExtentReports逻辑-LMLPHP

实际ExtentReports文件
java - 如何为Selenium WebDriver实现Advanced ExtentReports逻辑-LMLPHP

最佳答案

使用ExtentTest对象记录日志示例:
注意:更改您的@aftermethod

    public static ExtentReports extent;
    public static ExtentTest logger;
    @BeforeSuite(alwaysRun = true)
    public void config(){
        extent = new ExtentReports(path, true);
        extent.addSystemInfo("Host Name", System.getProperty("user.name"));
        extent.addSystemInfo("Environment", "API Testing");
        extent.loadConfig(new File(XMLPath));}

   @AfterMethod(alwaysRun = true)
public void getResult(ITestResult result) {
    if (result.getStatus() == ITestResult.SUCCESS) {
        logger.log(LogStatus.PASS, "Test case is passed " + result.getName());

    } else if (result.getStatus() == ITestResult.FAILURE) {
        logger.log(LogStatus.FAIL, "Test case is failed " + result.getName());
        logger.log(LogStatus.FAIL, "Test case is failed " + result.getThrowable());
    } else if (result.getStatus() == ITestResult.SKIP) {
        logger.log(LogStatus.SKIP, "Test case is Skiped " + result.getName());
    }
    extent.endTest(logger);
}

@AfterSuite(alwaysRun = true)
public void endReport() {
    extent.flush();
    // SendEmail.Email(ReportPath);
    extent.close();
    log.setinstanceNull();
}


使用ExtentTest对象记录测试用例中的测试步骤,以导入基类并使用ExtentTest对象,如下所示
样本测试用例

logger.log(LogStatus.INFO, message);
logger.log(LogStatus.ERROR, message);


要在开始测试用例之前获取测试用例的列表,请使用extent.startTest方法,其中extent是ExtentReports的对象

@Test(groups = { "sanityuserfolw" })
public void testcase() {

    logger = extent.startTest("TestCase Name", "test case summary");



    resp = part_study.sampletest(input);
    logger.log(LogStatus.INFO, "response is "+resp.getStatusCode());
    assertEquals(resp.getStatusCode(), 200, "Error in adding study " + resp.statusLine());

}

10-05 22:48