Generate HTML Report in Selenium WebDriver Project using Extent Reports Libraries
Here we will be going to talk about generating Html report after selenium scripts execution using TestNg and Extent Reports Libraries.
As HTML report is one of the most important part of the automation framework as it actually represents what actually happened in execution like how many of scripts got passed/failed/skipped etc.
There are many open source tools available on internet which creates customized HTML reports once scripts execution is done. However, here we will be going to generate highly customized HTML reports using Extent Reports jars. We are using following version in below example : extentreports-java-v2.41.0.
Pre-requisite
- Download extentreports-java-v2.41.0 Jars and import them into your project
- Import Selenium stand alone Jar into your Project
- Create a folder name as “HtmlReport” into your Project
- Create a folder name as “driver-servers” and put chromedriverserver.exe into it.
Example
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import com.relevantcodes.extentreports.ExtentReports; import com.relevantcodes.extentreports.ExtentTest; import com.relevantcodes.extentreports.LogStatus; public class GenerateReport { WebDriver driver; String appUrl; static ExtentReports reports; ExtentTest test; @BeforeClass public synchronized void initialize() { // Create an instance of ExtentsReports class and pass report storage // path as a parameter reports = new ExtentReports(System.getProperty("user.dir") + "/HtmlReport/index.html", true); } @BeforeTest public void setup() throws IOException { System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/driver-servers/chromedriver.exe"); driver = new ChromeDriver(); appUrl = "http://automate-apps.com"; } @Test public void testApp_1() throws IOException { try { // Start test. Mention test script name test = reports.startTest("test1", "Validate Page Navigation"); driver.manage().window().maximize(); // Launch URL driver.get(appUrl); test.log(LogStatus.INFO, "Application is launched"); // Validate Home page title Assert.assertEquals(driver.getTitle(), "Automate Apps | Way to learn Automation"); // Print log info in HTML report test.log(LogStatus.INFO, "Home Page Title Validated"); // Clicking on Contents tab driver.findElement(By.linkText("Contents")).click(); // Validating Contents Page title Assert.assertEquals(driver.getTitle(), "Contents Automate Apps"); // Print log info in HTML report test.log(LogStatus.INFO, "Contens Page Title Validated"); } catch (Throwable t) { // Print fail info in HTML report test.log(LogStatus.FAIL, t.getMessage()); String screenShotPath = System.getProperty("user.dir") + "/HtmlReport/Failure.png"; // Take screen shot of page takeScreenShot(driver, screenShotPath); // Attach screen shot in HTML report test.log(LogStatus.INFO, "Snapshot below: " + test.addScreenCapture(screenShotPath)); Assert.assertTrue(false, t.getMessage()); } } @AfterTest public void tearDown() { // Ending Test reports.endTest(test); // writing everything into HTML report reports.flush(); } @AfterClass public void clearingSetup() { // Quitting browser driver.quit(); } public void takeScreenShot(WebDriver driver, String filePath) { File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(scrFile, new File(filePath)); } catch (IOException e) { e.printStackTrace(); } } }
When you execute the above program, it will fail at following line :
driver.findElement(By.linkText("Contents")).click();
Now go to HtmlReport Folder in your project and open index.html file where you will see the script logs along with screen shot and lot of other details.
Refer Extent Report documentation for more information !!!
If you really like the information provided above, please don’t forget to like us on Facebook, you can also leave the comment.