bdd testing

ExtentReports Documentation

ExtentReports (by Anshoo Arora) is a HTML reporting library for Selenium WebDriver for Java and creates easy to use, attractive reports. It shows test and step summary, test steps and status in a toggle view for quick analysis.

Download

Download the jar from this link.

Maven / pom.xml

Simply add the following to your pom.xml:

<dependency>
  <groupId>com.relevantcodes</groupId>
  <artifactId>extentreports</artifactId>
  <version>1.4</version>
</dependency>

You can view Extent’s pom.xml here.

Basic Usage

ExtentReports is very simple to use. Below is some version-specific basic usage to get you started using this library.

You can also view a Selenium test that Madhu has created with usage at his blog.

// version 1.3+
import com.relevantcodes.extentreports.*;
 
public class Main {
    // *REQUIRED
    // put this in every class 
    // * Main.class will become TheClassName.class
    static final ExtentReports extent = ExtentReports.get(Main.class); 
 
    public static void main(String[] args) {
        extent.init("C:\\Extent.html", true);
 
        // *REQUIRED
        // startTest( testName )
        //    creates a toggle for the given test, adds all log events under it    
        extent.startTest("Main");
 
        // log(logStatus, stepName, details)
        extent.log(LogStatus.PASS, "StepName", "PASS Details"); 
 
        // log(LogStatus, details)
        extent.log(LogStatus.INFO, "This step shows usage of log(logStatus, details)");
 
        // report with snapshot
        // log(logStatus, stepName, details, screenCapturePath)
        extent.log(LogStatus.INFO, "Image", "Image example:", "C:\\img.png");
 
        // only report a snapshot, without status
        extent.attachScreenshot("pathtoImg.png", "This step attaches a screenshot without status.");
 
        // start other tests..
    }
}

It is not required to call endTest() version 1.0 onwards. However, calling it at the end of run session is still recommended.

Its not required to call the init method before each test. Its recommended to use “init” method only once, at the beginning of the run session to set the reporting path.

By default, the oldest test appears at the top. To change this behavior, or to allow the latest test to appear at the top, use the code below. Report started in one order will remain in that order unless the report is over-written or a new report is created.

extent.init(filePath, true, DisplayOrder.BY_LATEST_TO_OLDEST);

Initializing Report

To initialize, simple call the init method:

init(filePath, replaceExisting)
init(filePath, overwriteExisting, DisplayOrder)
init(filePath, overwriteExisting, GridType)
init(filePath, overwriteExisting, DisplayOrder, GridType)

where:
    filePath:  path of the file, in .htm or .html format
    overwriteExisting:  (true|false) 
        true:  the file will be replaced with brand new markup, 
                and all existing data will be lost. Use this option to create a 
                brand new report
        false:  existing data will remain, new tests will be appended to the existing 
                report
    DisplayOrder:  this determines the order in which your tests will be displayed
        BY_OLDEST_TO_LATEST (default) - oldest test at the top, newest at the end
        BY_LATEST_TO_OLDEST - newest test at the top, oldest at the end
    GridType:  determines the type of grid to be used
        STANDARD (default) - standard grid with 1 test per row
        MASONRY - creates a masonry style grid with 2 tests per row

Creating step logs

// log(logStatus, stepName, details)
extent.log(LogStatus.PASS, "StepName", "PASS Details"); 
 
// log(LogStatus, details) (VERSION 1.2+ only)
extent.log(LogStatus.INFO, "This step shows usage of log(logStatus, details)");
 
// log(logStatus, stepName, details, screenCapturePath)
extent.log(LogStatus.INFO, "Image", "Image example:", "C:\\img.png");

It is possible to log your execution steps in 3 ways:

Appending to an existing report

Easy! Simply use overwriteExisting = false setting to initialize and your tests will be appended to your existing report:

extent.init("path-to-file", false);

Adding a Test Description

// startTest(String testName, String testDescription)
extent.startTest("Test - With Description", "This description will show up under Test.");

Inserting a Snapshot

You can insert snapshot as a link or directly into the report using:

// log(LogStatus logStatus, String stepName, String details, String screenCapturePath)
extent.log(LogStatus.INFO, "Image", "Image example:", "C:\\img.png");

Attaching a Snapshot without Status

You can attach a snapshot to the report (v1.2+) without having to specify status using:

// void attachScreenshot(path, details)
extent.attachScreenshot("pathToImg.png", "Attach screenshot without status.");

Attaching a screen-shot is not counted as a step and will not show under Execution Info either.

Using Relative paths for snapshots

Relative paths starting with “.” and “/” characters are supported:

// ./pathToImg.png
extent.log(LogStatus.INFO, "Image", "Image example:", "./pathToImg.png");
 
// /pathToImg.png
extent.log(LogStatus.INFO, "Image", "Image example:", "/pathToImg.png");

If you using an absolute path, “file:///” will be automatically be appended for the image to load.

Inserting a Label

You can add labels to your log events using <span class='label labelName'>label message</span>.

There are 4 labels available:

  • success
  • failure
  • info
  • warn
extent.log(LogStatus.INFO, "Example: <span class='label success'>success</span>"); extent.log(LogStatus.INFO, "Example: <span class='label failure'>fail</span>"); extent.log(LogStatus.INFO, "Example: <span class='label info'>info</span>"); extent.log(LogStatus.INFO, "Example: <span class='label warn'>warning</span>");

You can use HTML anywhere in this report. Simply do this to insert a link:

extent.log(LogStatus.PASS, "Link", "Usage: <a href='http://relevantcodes.com'>link</a>.");

Inserting Custom HTML

Just like above, you can insert any HTML tag to your report:

extent.log(LogStatus.INFO, "HTML", "Usage: <span style='font-weight:bold;'>BOLD</span>");

Customization

You can customize the report as you want. Changes can be easily made to the overall CSS by bringing your own custom css, changes to the icons can be made by picking your own from font-awesome etc. Below is some basic usage to demonstrate this library’s customization.

Using custom CSS

You have options to use custom CSS directly (version 1.0+) in the document or bring in your own stylesheet.

// custom styles String style = "p{font-size:20px;}" + ".test{background-color:#000 !important;color:#fff !important;}";   extent.config().addCustomStyles(style);

// custom stylesheet extent.config().addCustomStylesheet("C:\\css.css");

Changing Report Title

It is possible to change the title of the report file (v1.2+) using:

extent.config().documentTitle("MyTestReport");

Themes*

ExtentReports can be heavily themed/modified to suit a layout as per your needs. The example below shows a Cucumber like color-scheme:

String css = "#topbar { background-color: #8bb1ec; }" + ".topbar-items-right span { color: white; }" + ".menu span { color: darkgreen; }" + ".menu-item-selected span { border-bottom: 1px solid green; }" + "#dashboard { background-color: transparent; }" + ".test { border: 1px solid lightseagreen; }" + ".description { background-color: transparent; border-left: 2px solid orange; padding: 2px 15px;}" + ".name { color: darkgreen; }" + ".extent-table { border: 1px solid #bbb; }" + ".extent-table th { background: none repeat scroll 0 0 olivedrab; color: #fff; }" + ".extent-table td { border-bottom: 1px solid #bbb; }";   extent.config().addCustomStyles(css);

The above will result in the following output. Its a small change, but quite different from the original.

Using custom JS

Just like having the ability to change document styles, you can also add your own custom scripts.

extent.config().insertJS("$('.test').click(function(){ alert('test clicked'); });");

Its possible to add/remove the Extent footer using the following configuration:

// remove the footer extent.config().useExtentFooter(false);   // use the footer extent.config().useExtentFooter(true);

Change status icons

Not a lot of people would do this, but you can choose to use your own icons for log status (PASS, FAIL, WARNING etc.) by choosing one of the icons from Fontawesome website: http://fortawesome.github.io/Font-Awesome/icons/.

extent.config().statusIcon(LogStatus.PASS, "check-circle");

Changing the top-level summary

You can remove or add your own summary by using the following config:

extent.config().reportHeadline("My custom report summary.");

Change chart title

It is possible (v1.4+) to customize the chart-title text by using the following config:

extent.config() .chartTitleText(Chart.TEST, "Your New Title") .chartTitleText(Chart.TEST_SET, "Your New Title");

Change Screenshot size

The default screenshot size attached to the report is 50%. It is possible to change screenshot size (v1.4+) by specifying a percentage:

extent.config().setImageSize("30%");

CallerClass display setting

It is now possible to hide the caller class by this simple setting:

extent.config().displayCallerClass(false);

Leave a Comment

Scroll to Top