No framework is complete without report feature, So this blog will help you to have a Create Advanced Test Report with Selenium Web Driver.
What are the ExtentReport
ExtentReports is an open-source reporting library useful for test automation and It can be easily integrated with major testing frameworks like TestNG etc. These reports are high rich HTML reports. It provides results in the form of PIE charts. Using Extent Reports we could generate custom logs, add snapshots. We could use external XML file to provide extra information.
Extent Reports in Selenium contain two major, frequently used classes:
ExtentReports class
ExtentTest class
The ExtentReports class generates HTML reports based on a path specified by the tester.Based on the Boolean flag, the existing report has to be overwritten or a new report must be generated. ‘True’ is the default value, meaning that all existing data will be overwritten.
Using these class, We create Advanced Test Report with Selenium Web Driver.
startTest: Executes preconditions of a test case
endTest: Executes postconditions of a test case
log: Logs the status of each test step onto the HTML report being generated
flush: Erases any previous data on a relevant report and creates a whole new report
A Test Status can be indicated by the following values:
Create a new JAVA class with the below code for Extent Reports.
public class ExtentReport implements IReporter {
private ExtentReports extent1;
private ExtentHtmlReporter extentHtmlReporter;
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
String outputDirectory) {
Properties prop = new Properties();
FileInputStream FileInputStream = null;
String reportPath = null;
try {
FileInputStream = new FileInputStream(
new File("src//main//resources//object.properties"));
prop.load(FileInputStream);
reportPath = prop.getProperty("ReportPath");
} catch (IOException e) {
e.printStackTrace();
}
extent1 = new ExtentReports(reportPath, true);
for (ISuite suite : suites) {
Map<String, ISuiteResult> result = suite.getResults();
for (ISuiteResult r : result.values()) {
ITestContext context = r.getTestContext();
try {
buildTestNodes(context.getPassedTests(), LogStatus.PASS);
} catch (Exception e) {
e.printStackTrace();
}
try {
buildTestNodes(context.getFailedTests(), LogStatus.FAIL);
} catch (Exception e) {
e.printStackTrace();
}
try {
buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);
} catch (Exception e) {
e.printStackTrace();
}
}
}
ExtentAventReporter reporter= new ExtentAventReporter(reportPath);
extent1.addSystemInfo("Tester", "Prajjawal Kansal");
extent1.flush();
extent1.close();
}
private void buildTestNodes(IResultMap tests, LogStatus status) throws Exception {
ExtentTest test1;
if (tests.size() > 0) {
for (ITestResult result : tests.getAllResults()) {
//testCaseCount++;
test1 = extent1.startTest("For "+((SheetColumnHeader)result.getParameters()[0]).getFirstname()+" details this test case ");
test1.setStartedTime(getTime(result.getStartMillis()));
test1.setEndedTime(getTime(result.getEndMillis()));
for (String group : result.getMethod().getGroups()){
test1.assignCategory(group);
}
if (result.getThrowable() != null) {
test1.log(status, " failed " + test1.addScreenCapture(System.getProperty("user.dir")+ "/test-output/Screenshot/Screenshot.png"));
} else {
test1.log(LogStatus.PASS, " passed");
}
extent1.endTest(test1);
}
}
}
private Date getTime(long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
}
How to print the description for each test case in Report
Create a New JAVA class, there we generate the getter or setter and string value for each column, So we print any column value in report. let’s see with code.
public SheetColumnHeader() {
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) { this.firstname = firstname; }
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getExpectedResult() {
return expectedResult;
}
public void setExpectedResult(String expectedResult) {
this.expectedResult = expectedResult;
}
@Override
public String toString() {
return "SheetColumnHeader{" +
"firstname='" + firstname + '\'' +
", lastname='" + lastname + '\'' +
", gender='" + gender + '\'' +
", number='" + number + '\'' +
", expectedResult='" + expectedResult + '\'' +
'}';
}
Using this line of code,This line will help to attach description for each test case.
By capturing screenshots, testers can better identify what went wrong when the software acted erroneously during a test. Capture screenshots only when a test fails, since they consume a lot of memory.
Create a method takeScreenshot which will implement the code to take the screenshot. This method will return the path where the screenshot is saved.