Search code examples
javaseleniumtestngextentreports

How to capture all test cases in extent report?


I have an issue in print all test cases in extent report. I have added two test cases in testcase file but only last test case is showing in the extent report. Below is my code. I am executing the test cases using TestNG.

public class TC02_Login extends BaseClass {

    ExtentReports extent;
    ExtentTest logger;
    WebDriver driver;

    @BeforeMethod
    public void createReport() {
        ExtentHtmlReporter reporter = new ExtentHtmlReporter("./Reports/finalReport.html");
        extent = new ExtentReports();
        extent.attachReporter(reporter);
    }

    @Parameters({ "userName", "password" })
    @Test(priority = 1)
    public void login(String userName, String password) throws InterruptedException, IOException {

        logger = extent.createTest("Login Module");
        objSeleutils.click(objLogin.lbl_signin);
        objSeleutils.enterText(objLogin.txt_email, userName);
        objSeleutils.enterText(objLogin.txt_password, password);
        objSeleutils.click(objLogin.btn_signup);

        boolean txt_welcome = objSeleutils.existsElement(objLogin.ddl_welcome);
        Assert.assertEquals(txt_welcome, true);
    }

    @Test(priority = 2)
    public void dashboard() {

        logger = extent.createTest("Dashboard Verification.");
        boolean txt_welcome = objSeleutils.existsElement(objLogin.btn_findPet);
        Assert.assertEquals(txt_welcome, false);
    }

    @AfterMethod
    public void teardown(ITestResult results) throws IOException {
        if (results.getStatus() == ITestResult.FAILURE) {
            String path = System.getProperty("user.dir") + "/Screenshots/" + "Fail_" + System.currentTimeMillis()
                    + ".png";
            String temp = objSeleutils.tackScreenShot(path);
            logger.fail(results.getThrowable().getMessage(), MediaEntityBuilder.createScreenCaptureFromPath(temp).build());
            logger.log(Status.FAIL, "Test Case Failed.");
        }

        if (results.getStatus() == ITestResult.SUCCESS) {
            String path = System.getProperty("user.dir") + "/Screenshots/" + "Pass_" + System.currentTimeMillis()
                    + ".png";
            String temp = objSeleutils.tackScreenShot(path);
            logger.log(Status.PASS, "Test Case Passed.", MediaEntityBuilder.createScreenCaptureFromPath(temp).build());
        }
        extent.flush();
    }
}

Here is my testNG file from where i am running my testcase.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Web" verbose="2">

<parameter name="browserName" value="Chrome" />
<parameter name="projectURL" value="http://sitename.com" />
<parameter name="userName" value="ashishsavaliya@gmail.com" />
<parameter name="password" value="*********" />

<test name="Test">          
        <classes>
            <class name='testCase.TC02_Login' />
        </classes>
  </test>
</suite>

Solution

  • It's because the logic that is in your @BeforeMethod should be in @BeforeClass or @BeforeSuite as you are re-creating the entire extent object with each new test as you have it now.