In our earlier post (Selenium reporting using extent 2.x), we have seen as a starting point how to generate report at end of test execution using 2.x version.
As extent report is becoming more popular and they are also trying to make it more flexible by providing frequent updates, so in this post, we will be discussing more in detail about the newly release version 3.0 (till now), which is of quite different in configuration (of course, concept remains same :D).
Find the required dependencies from following links
- Extent report 3.x
- Freemarker
- Mongodb bson
- Selenium and testNG related dependecies
Let’s look into the code implementation
1st thing, we need a utility or base class (ExtentManager.java) that takes care all the extent configuration, so that test cases will be using this ExtentManager.java to generate report.
package mypkg; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; public class ExtentManager { private static ExtentReports extent; private static ExtentTest test; private static ExtentHtmlReporter htmlReporter; private static String filePath = "./extentreport.html"; public static ExtentReports GetExtent(){ if (extent != null) return extent; //avoid creating new instance of html file extent = new ExtentReports(); extent.attachReporter(getHtmlReporter()); return extent; } private static ExtentHtmlReporter getHtmlReporter() { htmlReporter = new ExtentHtmlReporter(filePath); // make the charts visible on report open htmlReporter.config().setChartVisibilityOnOpen(true); htmlReporter.config().setDocumentTitle("QAV automation report"); htmlReporter.config().setReportName("Regression cycle"); return htmlReporter; } public static ExtentTest createTest(String name, String description){ test = extent.createTest(name, description); return test; } }
Create testcases, log results or steps to generate report
public class TC1 { ExtentReports extent; ExtentTest test; WebDriver driver; @BeforeClass public void M1(){ extent = ExtentManager.GetExtent(); System.setProperty("webdriver.gecko.driver", "./geckodriver.exe"); driver = new FirefoxDriver(); } @Test public void checkHome() { try{ driver.get("http://www.qavalidation.com/"); //test = extent.startTest("OpenUT", "Verify HomePage");//earlier version test = extent.createTest("QAVsite", "Verify HomePage"); if(driver.getTitle().contains("QA manual")){ //test.log(LogStatus.PASS, driver.getTitle() +" contain "+"QA & Validation" );//earlier version //test.log(Status.PASS, driver.getTitle() +" contain "+"QA & Validation"); //or test.pass(driver.getTitle() +" contain "+"QA manual"); //test.log(Status.INFO, "Snapshot" + test.addScreenCaptureFromPath("./1.jpg")); } else //test.log(LogStatus.FAIL, driver.getTitle() +" doesn't contain "+"QA & Validation" );//earlier version test.log(Status.FAIL, driver.getTitle() +" doesn't contain "+"QA manual" ); }catch(Exception e){test.log(Status.ERROR, e.getMessage());} } @Test public void checkFail(){ test = extent.createTest("Testing how fail works"); //test.log(Status.INFO, "fail check started"); test.fail("Test fail"); } @AfterClass public void tear() { //extent.endTest(test);//earlier version extent.flush(); extent.close(); driver.quit(); } }
run the TC1.java, refresh your project, you will see extentreport.html created, open html page in a browser to see the extent report.
Explanation :
in ExtentManager.java, we are getting the objects of ExtentReports, ExtentHtmlReporter and ExtentTest , so separate test cases can use those objects to send test results to generate report.
each test case will have
- @BeforeClass – get the ExtentReports object and some test case specific code statements.
- @Test – actual test case functionality.
- @AfterClass – to write the test case results in to html (extent.flush())
All other code statements are explained with below screenshot
Configuration :
Extent report can be customized or configured by using ExtentHtmlReporter.config() (e.g – setDocumentTitle, setReportName) or by using config.xml and load it at run time.
NOTE: In the above code implementation, we saw how to configure extent report using ExtentHtmlReporter.config()
Now let’s see how we can load and cofigure report using config.xml
a typical config.xml –
<?xml version="1.0" encoding="UTF-8"?> <extentreports> <configuration> <!-- report theme --> <!-- standard, dark --> <theme>standard</theme> <!-- document encoding --> <!-- defaults to UTF-8 --> <encoding>UTF-8</encoding> <!-- protocol for script and stylesheets --> <!-- defaults to https --> <protocol>https</protocol> <!-- title of the document --> <documentTitle>QAV report</documentTitle> <!-- report name - displayed at top-nav --> <reportName>Regression</reportName> <!-- location of charts in the test view --> <!-- top, bottom --> <testViewChartLocation>bottom</testViewChartLocation> <!-- custom javascript --> <scripts> <![CDATA[ $(document).ready(function() { }); ]]> </scripts> <!-- custom styles --> <styles> <![CDATA[ ]]> </styles> </configuration> </extentreports>
Do required changes to the config.xml and place under your project folder, and change the ExtentManager.java a bit to use the xml file.
private static ExtentHtmlReporter getHtmlReporter() { htmlReporter = new ExtentHtmlReporter(filePath); htmlReporter.loadXMLConfig("./config.xml"); return htmlReporter; }
References –
github – extent report in java
https://github.com/anshooarora/extentreports-java/issues/652
Update –
Updated GetExtent() under class ExtentManager
if(extent != null)
return extent; \\to avoid creating new instance of html file at every test run
I have multiple class files with multiple methods in it. I need one report for the entire run but the all reports are overwritten and report for only last class is visible. I tried changing extent = new ExtentReports(); to extent = new ExtentReports(path, false); but its not allowing me to do it
Workaround to your problem –
Download extentreports-3.0.1.jar and replace existing jars with this latest to the current project, and add htmlReporter.setAppendExisting(true); under the GetExtent() method.
NOTE: make sure every time you execute tests, create an empty html file or else it will append even all your prev results.
I took your source code to generate report. I just created the same ExtentManager.java
I have my own test . when i execute I am not getting any error but the output is not generated.
I am using extentreports 3.0.2. I am new to testng and extent reports.
Could you please help me
Hello, can u please share your code or mail me on sunilpatro1985@gmail.com, let me chk and revert you.
Do you not have to use endTest(testName) anymore?
Not required, extent.flush() will do that for you, are you getting any issues generating report with out endtest(testname)?
can we execute Jmeter script through this extent class and generate extent reports
Thank you sunilpatro1985 · , this was really helpful!
But I have a problem with encoding. All my descriptions in cyrillic, last running test case is shown in report with cyrillic. But the other test which was run before shown like symbols (question marks).
I change config xml cp1251 but it doesn’t help
Hello AnsooArora,
I want to generate extent report according to date wise, right now it is overtire result in same file, Please help me
Thanks
Gangotri SIkheria
gangotrisikheria@gmail.com
Create a variable in extentmanager.java which will hold the date and time and append this variable to html file path, so the html file will not overwrite every time you run test and generate report…
Hope that helps!
There is certainly a great deal to learn about this topic.
I really like all the points you have made.
I get the error as FAILED CONFIGURATION: @AfterTest tearDown
java.lang.NoClassDefFoundError: org/bson/types/ObjectId
code is below
package AutFramework;
import static org.testng.Assert.assertTrue;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class AdvanceReporting {
WebDriver driver;
ExtentHtmlReporter htmlReports;
ExtentReports extent;
ExtentTest test;
String fileName = System.getProperty(“user.dir”)+”/test_output/HtmlTestResults.html”;
@BeforeTest
public void setUp() {
htmlReports = new ExtentHtmlReporter(fileName);
extent = new ExtentReports();
extent.attachReporter(htmlReports);
htmlReports.config().setReportName(“Check”);
htmlReports.config().setTheme(Theme.STANDARD);
htmlReports.config().setTestViewChartLocation(ChartLocation.BOTTOM);
htmlReports.config().setDocumentTitle(“HTMLReporttestresult”);
}
@Test(priority=0)
public void openBrowser()
{
test = extent.createTest(“TC_1 = Open Chrome driver”);
test.log(Status.INFO, “Test case execution is started”);
System.setProperty(“webdriver.chrome.driver”,”C:\\Users\\a0136300\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
test.log(Status.INFO, “Chrome Browser is open”);
}
@Test(priority=1)
public void OpenGoogleurl() {
test= extent.createTest(“TC_2 = Open Google url”);
driver.get(“https://www.google.com”);
driver.manage().window().maximize();
test.log(Status.PASS, “Google url is open”);
}
@Test(priority=2)
public void Verifypagetitle() {
test = extent.createTest(“TC_3= Verify the title of url”);
String title = driver.getTitle();
assertTrue(title.equals(“Googleee”));
driver.close();
}
@AfterTest
public void tearDown()
{
extent.flush();
}
@AfterMethod
public void checkresult(ITestResult testResults) {
if(testResults.getStatus()==ITestResult.FAILURE) {
test.log(Status.FAIL, “Test case is failed because”);
test.log(Status.FAIL, testResults.getThrowable());
} else if(testResults.getStatus()==ITestResult.SUCCESS) {
test.log(Status.PASS, “Test case is passed”);
} else if(testResults.getStatus()==ITestResult.SKIP) {
test.log(Status.SKIP, testResults.getThrowable());
}
}
}
Screenshots generated on extentreport gets blank/broken after sending the report to some other machine or to email. Is there any solution for this?