Script Extensions

You can extend the default reports by using report scripts that are available for test suite and test case reports. ReadyAPI runs report scripts each time it creates a report. To work with the script, use the script editor on the Report tab of the corresponding editor:

Report Script tab

Report scripts can use the following objects:

  • log – an object for interacting with the log.

  • report – an object that holds all data used to populate the report. Its members allows you to add or adjust data and subreport data the report engine uses to fill the report template.

  • params – an object that contains all defined report parameters and their values.

  • Also, scripts can access an object that represents the current test item (testSuite or testCase).

Mostly, when working with your report data, you will use the report and params objects.

Below is information on how to perform some useful tasks by using the report script.

The params object allows you to modify parameters declared in a report template or on the Report Parameter tab.

To access a parameter, specify its name after the period:

For example, you define an integer parameter in a report template:

Now, you can change it from script by using the following expression:

Each default report includes some metrics produced by the MetricsReport subreport. This subreport shows the execution-related metrics. You can include extra metrics in this report by using the report.addMetric() method. For example, the following code snippet adds the version of the operating system:

By default, new metrics are added to the Base Metrics section. You can also add them to a custom section. The code snippet below shows how to list all the system properties under the System Properties category.

You use subreports to create a report within the main report. Each subreport uses a template and provides a data source to include the needed data in the parent report.

Using the report object you can access an existing subreport as well as create new ones. The example below shows how to create a subreport containing system properties.

go.gifCreating a Helper Class

go.gifCreating a Subreport Template

go.gifAdding a Subreport to a Report Template

go.gifGenerating Data

go.gifMaking a Subreport Optional

Creating a Helper Class

A system property consists of two parts: the name and value. To pass it to a subreport, you can use a helper class that has two variables: name and value. ReadyAPI passes this class to the subreport template for each data record. To declare the class, open the Report script tab and enter the following code:

Creating a Subreport Template

  1. From the main menu, select Project > Reporting.

    Reporing option
  2. In the Reporting dialog, switch to the Subreports tab.

    Subreport tab
  3. Click add.png to create a new subreport template.

  4. Specify the properties of the new template:

    Add subreport dialog
  5. Enter the following text as the new template content:

    The field elements represent a piece of data obtained from a data source. The name attribute of these fields must coincide with variable names declared in a class representing the data record. In this example, two fields, name and value, match variables declared in the NVPair class on the previous step.

    To insert field values, use the $F{<field name>} expression. In the example above, the $F{name} expression inserts the value of the name variable, and $F{value} inserts the value of the variable.

Adding a Subreport to a Report Template

To add a subreport to a report, use the subreport element of the report template. To do this, perform the following steps:

  • Switch to the Available Reports tab of the Reporting dialog.

    Available Reports tab
  • Select the template to which you want to add a template. For example, the standard Test Case Report.

  • Insert the following text to a band element, which is a child element of the detail element:

    Important

    Make sure the value of the height attribute of the band element is greater than the value of the height attribute of the subreport footer element. In this example, it must be greater than 418 (the sum of the y and height values).

    The following elements specify subreport details:

    • The dataSourceExpression element specifies the data source that will feed a subreport with data.

    • The subreportExpression element specifies the used subreport template.

  • To pass a data source to the subreport, we use the SystemProperties parameter. To declare the parameter in the template, specify a new parameter element at the beginning of the template:

Generating Data

To grab data to show and produce the needed data source, create a custom SystemPropertiesSubReport class.

  1. Add the following code at the beginning of the Report script

  2. To build a data source and pass it to the report template, use the following code:

To test the created subreport, click Report and select the template to which you are adding the subreport:

Create Report dialog

Making a Subreport Optional

You can make this subreport optional:

  1. Include the following parameter element at the end of parameters element in the main template:

  2. Add the following conditional to the subreport element:

Now when you select the created template in the Create Report dialog, you can select whether to include this subreport in the report:

Create Report dialog with added parameter

See Also

Publication date: