Ashish Savaliya
Ashish Savaliya

Reputation: 85

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="[email protected]" />
<parameter name="password" value="*********" />

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

Upvotes: 1

Views: 4726

Answers (2)

Ishita Shah
Ishita Shah

Reputation: 4035

As you are defining in BeforeMethod, It will overwrite with Last @Test. You need to pass some differentiate to have both reports.

like, You can use Count variable

public class TC02_Login extends BaseClass {

        ExtentReports extent;
        ExtentTest logger;
        WebDriver driver;
        int count = 1;

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

    @AfterMethod
    public void teardown(ITestResult results) throws IOException {
    count++;
    }
}

There are many ways to do so. One of the way, which you can use is to invoke each Report in every @Test with there relevant Test name.

Upvotes: 3

Bill Hileman
Bill Hileman

Reputation: 2838

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.

Upvotes: 2

Related Questions