Skip to content

Usage Guide

Anshoo Arora edited this page Jul 6, 2020 · 5 revisions

Reporters

The information you create during your runs can be published to disk or saved to a database. A reporter defines the destination and you must attach a reporter to ExtentReports in order to view reports. The default version 5 reporter is ExtentSparkReporter.

ExtentReports extent = new ExtentReports();
ExtentSparkReporter spark = new ExtentSparkReporter("target/Spark.html");
extent.attachReporter(spark);

Report filters for Status/Tag specific reports

It is now possible to create separate reports for each status (or a group of them). For example, 2 reports can be created per run session to

  1. view all tests
  2. view only failed ones.

The example below shows just that:

// will only contain failures
ExtentSparkReporter sparkFail = new ExtentSparkReporter("target/SparkFail.html")
  .filter()
    .statusFilter()
    .as(new Status[] { Status.FAIL })
  .apply();
// will contain all tests
ExtentSparkReporter sparkAll = new ExtentSparkReporter("spark/SparkAll.html");
extent.attachReporter(sparkFail, sparkAll);

Choose which views to display, in a particular order

It is now possible to select the views and their order. For example: if you want the Dashboard view to be the primary view, followed by Tests, you can use the snippet below:

ExtentSparkReporter spark = new ExtentSparkReporter("spark/spark.html")
  .viewConfigurer()
    .viewOrder()
    .as(new ViewName[] { ViewName.DASHBOARD, ViewName.TEST })
  .apply();

The above will limit the report to 2 views, with DASHBOARD view the primary one, followed by TEST. No other views will be displayed. Default setting is to display all views in this order:

  1. Test
  2. Tag
  3. Device
  4. Author
  5. Exception
  6. Dashboard

Create Tests

Tests are created using ExtentReports::createTest. The createTest method returns a ExtentTest object which can be used to publish logs, create nodes, assign attributes (tags, devices, authors) or attach screenshots.

ExtentTest test = extent.createTest("MyFirstTest");
test.pass("Pass");

// fluent
ExtentTest test = extent.createTest("MyFirstTest").pass("Pass");

Nodes

ExtentReports creates the test and returns an ExtentTest object, which can create nodes using createNode.

ExtentTest test = extent.createTest("MyFirstTest");
ExtentTest node = test.createNode("Node");
node.pass("Pass");

// fluent
ExtentTest node = extent.createTest("MyFirstTest")
  .createNode("Node")  
  .pass("Pass");

BDD

BDD hierarchy can be created using Gherkin classes provided by the ExtentReports library:

ExtentTest feature = extent.createTest(Feature.class, "Refund item");
ExtentTest scenario = feature.createNode(Scenario.class, "Jeff returns a faulty microwave");
scenario.createNode(Given.class, "Jeff has bought a microwave for $100").pass("pass");
scenario.createNode(And.class, "he has a receipt").pass("pass");
scenario.createNode(When.class, "he returns the microwave").pass("pass");
scenario.createNode(Then.class, "Jeff should be refunded $100").fail("fail");

The above can also be written without specifying the BDD class by passing the keyword:

ExtentTest feature = extent.createTest(new GherkinKeyword("Feature"), "Refund item");
ExtentTest scenario = feature.createNode(new GherkinKeyword("Scenario"), "Jeff returns a faulty microwave");
scenario.createNode(new GherkinKeyword("Given"), "Jeff has bought a microwave for $100").pass("pass");
scenario.createNode(new GherkinKeyword("And"), "he has a receipt").pass("pass");
scenario.createNode(new GherkinKeyword("When"), "he returns the microwave").pass("pass");
scenario.createNode(new GherkinKeyword("Then"), "Jeff should be refunded $100").fail("fail");

Gherkin Dialect

To specify the dialect of your Gherkin keywords:

extent.setGherkinDialect("de");

Remove Tests

Use removeTest to remove a test which was created using createTest or createNode.

ExtentTest test = extent.createTest("Test").fail("reason");
extent.removeTest(test);

// or remove using test name
extent.removeTest("Test");
ExtentTest test = extent.createTest("Test");
ExtentTest node = test.createNode("Node").fail("reason");
extent.removeTest(node);

// or remove using test name
extent.removeTest("node");

Logging

It is possible to create log with text details, Exception/Throwable, ScreenCapture or custom Markup using MarkupHelper.

Details

ExtentTest test = extent.createTest("MyFirstTest");
test.pass("Text details");
test.log(Status.PASS, "Text details");

Exceptions

Throwable t = new RuntimeException("A runtime exception");
ExtentTest test = extent.createTest("MyFirstTest");
test.fail(t);
test.log(Status.FAIL, t);

ScreenCapture

ExtentTest test = extent.createTest("MyFirstTest");

// reference image saved to disk
test.fail(MediaEntityBuilder.createScreenCaptureFromPath("img.png").build());

// base64
test.fail(MediaEntityBuilder.createScreenCaptureFromBase64String("base64").build());

Markup

More information on this topic can be found in the MarkupHelper section.

String json = "{'foo' : 'bar', 'foos' : ['b','a','r'], 'bar' : {'foo':'bar', 'bar':false,'foobar':1234}}";
test.info(MarkupHelper.createCodeBlock(json, CodeLanguage.JSON));

Custom Logs

You can create your own custom logs, tables with custom headers, pass your POJOs directly to be converted into a table etc. You can also specify any CSS classes to be applied on the table, like in the below example with "table-sm" (a bootstrap table class).

public class MyCustomLog {
  private List<Object> names = Arrays.asList("Anshoo", "Extent", "Klov");
  private Object[] favStack = new Object[]{"Java", "C#", "Angular"};
  @MarkupIgnore
  private List<Object> ignored = Arrays.asList("Ignored", "Ignored", "Ignored");
  private Map<Object, Object> items = new HashMap<Object, Object>() {
      {
          put("Item1", "Value1");
          put("Item2", "Value2");
          put("Item3", "Value3");
      }
  };
}
extent.createTest("GeneratedLog")
    .generateLog(Status.PASS, MarkupHelper.toTable(new MyCustomLog(), "table-sm"));
Clone this wiki locally