Last updated: Jul 16, 2020 14:08
Create an instance of the reporting client
Create an instance of the ReportiumClient used to log test start/step/end events:
function setupReportingClient(webdriver) { // Setup Perfecto reporting client const perfectoExecutionContext = new Reporting.Perfecto.PerfectoExecutionContext({ webdriver }); 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.
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 specific TestContext instance as part of the testStart() invocation, as shown below.
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
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 )
Note: 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.
...//selenium code reportingClient.stepStart('Do this'); ...//selenium code reportingClient.stepEnd(); // the message parameter is optional
Stop the test
...//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.
// 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
//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.
Protractor guide
Click here for a Getting started with Protractor guide.