JavaScript

Download the Reporting SDK client for your programming languages and framework from here.

Smart Reporting setup

The following code changes are required to include reporting in your test:

The below snippets are a standalone script that does not use any execution testing framework.

Mandatory

Create the reporting client

Add the following require statement to the test script:

Copy
const Reporting = require('perfecto-reporting');

Create an instance of the reporting client

Create an instance of the ReportiumClient used to log test start/step/end events:

Copy
function setupReportingClient(webdriver) {
  // Setup Perfecto reporting client
  const perfectoExecutionContext = new Reporting.Perfecto.PerfectoExecutionContext({
    webdriver
  });
  return new Reporting.Perfecto.PerfectoReportingClient(perfectoExecutionContext);
}
Important: For users of admc/wd for Node.js: When creating the reporting client instance, use the following code:
Copy
function setupReportingClient(webdriver) {
   const perfectoExecutionContext = new Reporting.Perfecto.PerfectoExecutionContext({
      webdriver: {
         executeScript: (command, params) => webdriver.execute(command, [params]),
         getCapabilities: webdriver.getCapabilities
      }
    });
   return new Reporting.Perfecto.PerfectoReportingClient(perfectoExecutionContext);
}

Optional changes (but highly recommended)

Add reporting tags

Add Tags are used as a freestyle text that is used for filtering in the Reporting app.
For example: tags: ['optional tag 1', 'optional tag 2'] 

Add CI job information

Job information is used to add your test runs to the CI Dashboard. Use the Reporting.Model.Job method, supplying the Job Name, Job Number, and Branch Name, when creating the Reporting-client instance.

Copy
function setupReportingClient(webdriver) {
  // Setup Perfecto reporting client
  const perfectoExecutionContext = new Reporting.Perfecto.PerfectoExecutionContext({
    webdriver,
    // Job method accepts a JSON list with three optional properties
    job: new Reporting.Model.Job({
        jobName: "Job name"
        buildNumber: 1234
        branch: "Branch name"        }), // optional
    project: new Reporting.Model.Project("Project name" , "v0.1"), // optional
    tags: ['optional tag 1', 'optional tag 2'] // optional
  });
  return Reporting.Perfecto.PerfectoReportingClient(perfectoExecutionContext);
}

Add custom fields

Create new CustomField instances and add them to your test runs, either to the PerfectoExecutionContext when setting up the ReportingClient or to the specificTestContextinstance as part of the testStart() invocation, as shown below.

Copy

Custom fields in PerfectoExecutionContext

function setupReportingClient(webdriver) {
  // Setup Perfecto reporting client
  const perfectoExecutionContext = new Reporting.Perfecto.PerfectoExecutionContext({
    webdriver,
...
    customFields: [new Reporting.Model.CustomField('testIndex', '1')] // optional custom fields
  });
  return Reporting.Perfecto.PerfectoReportingClient(perfectoExecutionContext);
}

Add test information and steps

Start a new test

Copy
reportingClient.testStart('Start test: testSearch', new Reporting.Perfecto.PerfectoTestContext(
  ['additionalTag'],          // optional tags for execution context tags
  [new Reporting.Model.CustomField('testIndex', '1')] // optional custom fields that will be added and override duplicated fields from execution context custom fields
)

The PerfectoTestContext is a structure that adds context-sensitive information to the test started by this command. This is optional, and can be used to add tags and custom fields that will later be used by Smart Reporting when showing the test reports.

Add test steps

Separate your test into actions using test steps.

Copy
...//selenium code
reportingClient.stepStart('Do this');
...//selenium code
reportingClient.stepEnd(); // the message parameter is optional

Stop the test

Copy
...//selenium code
reportingClient.stepStart('Do that');
    // More Selenium code...
reportingClient.stepEnd(); // the message parameter is optional
    reportingClient.testStop({
      status: Reporting.Constants.results.passed
    });
  } catch (err) {
    reportingClient.testStop({
      status: Reporting.Constants.results.failed,
      message: JSON.stringify(err)
    });
  }
}

Add assertions to the execution report

At various points within a test execution, the script may perform verification of different test conditions. The result of these verification may be added to the Test Report by using the reportingAssertion() method of the ReportiumClient instance. When using this method, the script includes two parameters:

  • A message string - that will be used to label the assertion.
  • A Boolean - indicates the result of the verification operation.
Copy
// Step3: Validate the text 'images' appears in the URL
reportingClient.stepStart('Step3: Validate the text "images" appears in the URL');
const currentUrl = driver.getCurrentUrl();    
reportingClient.reportiumAssert('Assert url', currentUrl == expectedUrl);
reportingClient.stepEnd('Step 3: Finished');

Get the report URL

Copy
//retrieve the report URL
reportingClient.getReportUrl()
        .then((url) => {
          console.log(`Report url ${url}`);
        });

GitHub samples

Browse the Perfecto GitHub report for complete JavaScript reporting samples.