19
Oct

Generate ExtentReport for multiple TestNG test scenarios

Dependencies:

extentreports-3.0.0.jar
freemarker-2.3.28.jar
mongo-java-driver-2.10.1.jar

or


org.jsoup
jsoup
1.8.3


org.xerial
sqlite-jdbc
3.8.11.1


org.freemarker
freemarker
2.3.23


org.mongodb
mongodb-driver
3.0.4

Step 1:  Create a baseclass which implements setup and teardown of extent reports

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class BaseTest {
public static ExtentHtmlReporter htmlReporter;
public static ExtentReports extent;
public static ExtentTest test;

@BeforeSuite
public void setUp() {
htmlReporter = new ExtentHtmlReporter(System.getProperty(“user.dir”) + “/test-output/MyOwnReport.html”);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);

extent.setSystemInfo(“OS”, “Mac Sierra”);
extent.setSystemInfo(“Host Name”, “xxxx”);
extent.setSystemInfo(“Environment”, “QA”);
extent.setSystemInfo(“User Name”, “xxxxxxx”);

htmlReporter.config().setChartVisibilityOnOpen(true);
htmlReporter.config().setDocumentTitle(“Automation Test Report for xxx environment”);
htmlReporter.config().setReportName(“Test Report for Sanity Environment”);
htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
htmlReporter.config().setTheme(Theme.STANDARD);
}

@AfterMethod
public void getResult(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());
}
}

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

 

Step 2:  implement test classes (which implements actual tests) eg.

import org.testng.Assert;
import org.testng.annotations.Test;

public class Functionality1 extends BaseTest {
@Test
public void functionality1Test1() {
test = extent.createTest(“functionality1Test1”);
Assert.assertTrue(1 > 0);
}

@Test
public void functionality1Test2() {
test = extent.createTest(“functionality1Test2”);
Assert.assertEquals(“xx”, “xx”);
}

@Test
public void functionality1Test3() {
test = extent.createTest(“functionality1Test3”);
Assert.assertEquals(“xx”, “xx”);
}
}

 

Step 2 (next class which implements TestNG testcases)

import org.testng.Assert;
import org.testng.annotations.Test;

public class Functionality2 extends BaseTest {
@Test
public void functionality2Test1() {
test = extent.createTest(“functionality2Test1”);
Assert.assertTrue(1 > 0);
}

@Test
public void functionality2Test2() {
test = extent.createTest(“functionality2Test2”);
Assert.assertEquals(“xx”, “xx”);
}

@Test
public void functionality2Test3() {
test = extent.createTest(“functionality2Test3”);
Assert.assertEquals(“xx”, “xx”);
}
}

 

Step 3:  Update testng.xml to include above classes as tests to run

 

<suite name=”TestAll”>

<test name=”order”>
<classes>
<class name=”tempExtentReports.Functionality1″ />
<class name=”tempExtentReports.Functionality2″ />
</classes>
</test>

</suite>

Step 4:  Run the testng.xml as “TestNG suite”