Selenium扩展区报告框架未生成失败状态

holgip5t  于 2023-02-04  发布在  其他
关注(0)|答案(3)|浏览(138)

目前,我正在使用Selenium TestNG和Extent Reports框架进行报告。我面临着一个问题,即我的所有测试用例都显示通过而不显示失败。

@BeforeTest
public void setUp()
{
    //where we need to generate the report
    htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir")+"/test-output/MyReport.html");
    extent = new ExtentReports();
    extent.attachReporter(htmlReporter);

    // Set our document title, theme etc..
    htmlReporter.config().setDocumentTitle("My Test Report");
    htmlReporter.config().setReportName("Test Report");
    htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
    htmlReporter.config().setTheme(Theme.DARK);
}

@Test
public void On_Page_IM() throws InterruptedException {
    test = extent.createTest("On_Page_IM");
    wd.manage().window().maximize();        
    Thread.sleep(10000);       
    test.log(Status.INFO,"On page intent media: Show");
}

@AfterSuite
public void tearDown()
{
    extent.flush();
    wd.quit();
}

nuypyhwy

nuypyhwy1#

添加此代码后,问题解决了

@AfterMethod
public void getResult(ITestResult result)
{
    if(result.getStatus()==ITestResult.FAILURE)
    {
        test.log(Status.FAIL, result.getThrowable());

    }
   // extent.endTest(test);
}
isr3a4wc

isr3a4wc2#

只需在测试结束时添加以下代码

@AfterMethod
public void AfterMethod(ITestResult result) {

    if (result.getStatus() == ITestResult.FAILURE) {
        test.log(Status.FAIL,
                MarkupHelper.createLabel(result.getName()
                        + " Test case FAILED due to below issues:",
                        ExtentColor.RED));
        test.fail(result.getThrowable());
    } else if (result.getStatus() == ITestResult.SUCCESS) {
        test.log(
                Status.PASS,
                MarkupHelper.createLabel(result.getName()
                        + " Test Case PASSED", ExtentColor.GREEN));
    } else {
        test.log(
                Status.SKIP,
                MarkupHelper.createLabel(result.getName()
                        + " Test Case SKIPPED", ExtentColor.ORANGE));
        test.skip(result.getThrowable());
    }
}

@AfterTest
public void AfterTest() {

    extent.flush();

}
bxjv4tth

bxjv4tth3#

Selenium Web驱动程序4 + JUnit 5 +扩展区报告5(JDK 17)

我使用了thisthis来编写现代的JUnit 5解决方案,因为大多数示例都是针对旧版TestNG的。ExtentHtmlReporter也已被弃用。

abstract class BaseTest {
    private static ExtentReports extent; // Prevent overwriting reports per test
    protected WebDriver driver; // WebDriver should never be static

    @RegisterExtension
    protected AfterEachExtension afterEachExtension = new AfterEachExtension();

    @BeforeAll
    public static void beforeAll() {
        extent = new ExtentReports();
        spark = new ExtentSparkReporter("target/spark-reports/index.html");
        extent.attachReporter(spark);
        spark.config(
            ExtentSparkReporterConfig.builder()
                .theme(Theme.DARK)
                .documentTitle("Extent Reports")
                .build()
        );
    }

    @BeforeEach
    public void beforeEach() {
        driver = // Initialise driver
    }

    @AfterEach
    public void afterEach() {
        AfterEachExtension.setExtent(extent);
        afterEachExtension.setDriver(driver);
    }

    @AfterAll
    public static void afterAll() {
        extent.flush();
    }
}

import com.aventstack.extentreports.Status;
// Other imports

public class AfterEachExtension implements AfterEachCallback {
    private static ExtentReports extent;
    private WebDriver driver;

    public static void setExtent(ExtentReports extent) {
        AfterEachExtension.extent = extent;
    }

    public void setDriver(WebDriver driver) {
        this.driver = driver;
    }

    @Override
    public void afterEach(ExtensionContext context) throws Exception {
        var test = extent.createTest(context.getDisplayName());
        context.getExecutionException().ifPresent(value -> test.log(Status.FAIL, value));
        driver.quit();
    }
}

相关问题