JavaScript
Make sure you have downloaded the Perfecto Smart Reporting SDK client for your programming languages and framework from here.
Perform the following setup tasks to integrate your tests with Smart Reporting.
beforeFeature()
and afterFeature()
in Cucumber or before()
and after()
in Mocha).
For JavaScript reporting samples, see the Perfecto GitHub repository.
Click a task to expand its steps.

Add the following import
statement to your test script.

The following generic JavaScript function generates an instance of the reporting client. Update your test to call this function from the lifecycle hooks of your respective framework.
For example, in a Cucumber-based framework, call this function from the beforeFeature()
hook; in a Mocha-based framework, call this function from the before()
hook.
Add this function to your framework hooks to ensure an instance of the reporting client is created.
async function setupReportingClient(browser, job_name, job_number, job_branch, project_name, project_version, tags, customFields) {
return new Reporting.Perfecto.PerfectoReportingClient(new Reporting.Perfecto.PerfectoExecutionContext({
webdriver: {
executeScript: async (command, params) => {
await browser.execute(command, params);
}
},
// Job method accepts a JSON list with three optional properties
job: new Reporting.Model.Job({
jobName: job_name,
buildNumber: parseInt(job_number),
//Number(jobNumber),
branch: job_branch
}),
// optional
project: new Reporting.Model.Project(project_name, project_version),
// optional - Add global reporting tags
tags: tags,
customFields: customFields
}));
}
Where:
-
browser
is the instance of the webdriver. For WDIO, this will be the browser object. -
job_name
is the CI job name -
job_number
is the CI job number (needs to be a number) -
job_branch
is the branch name (optional) -
project_name
is the name of the project (optional) -
project_version
is the version of the project (optional) -
tags
is the array of strings representing the global test tags (optional) -
customFields
is the array of custom fields of type object
await setupReportingClient(browser, "My Job", 1, "My job branch", "Revamp UI", "v1.0", ["Revamp_UI","v1.0"],[new Reporting.Model.CustomField('EpicVersion', '1001')] )

The following generic JavaScript function starts a test in Perfecto Smart Reporting. This function is called when a new test is started and can be called from the framework-specific start test hook.
For example, in Cucumber, this would be the beforeScenario()
method, and in Mocha, this would be the beforeEach()
method.
Add this function to test (start hook) after initializing the reporting client.
async function startTest(reportingClient, testName, tags, customFields ){
await reportingClient.testStart(testName, new Reporting.Perfecto.PerfectoTestContext(
tags, // optional tags for execution context tags
customFields // optional custom fields that will be added and override duplicated fields from execution context custom fields
));
}
where:
-
reportingClient
is the reporting client object (initialized as described in Step 2) -
testName
is the string representing the name of current test -
tags
is the array of strings representing the local tags applied to the test -
customFields
is the array of CustomField type objects
Usage example
await startTest(reportingClient, "My Test", ["Tag1", "Tag2"], [new Reporting.Model.CustomField('jira_story_id', '2001')]);

Two functions report the status of the test to Perfecto Smart Reporting, whether it passed or failed. The relevant function needs to be called after each test completes but before the driver is released.
-
The following generic JavaScript function reports the status of the current test in Perfecto as
Pass
.CopyGeneric JavaScript for a successfull test (Pass)async passTheTest(reportingClient){
await reportingClient.testStop({
status: Reporting.Constants.results.passed
});
}where:
-
reportingClient
is the instance of the reporting client created by the function in Step 2
-
-
The following generic JavaScript function reports the status of the current test in Perfecto as
Fail
.CopyGeneric JavaScript for an unsuccessful test (Fail)async failTheTest(reportingClient, errMessage){
await reportingClient.testStop({
status: Reporting.Constants.results.failed,
message: JSON.stringify(errMessage)
});
}where:
-
reportingClient
is the instance of the reporting client created by the function in Step 2 -
errMessage
is the string representing the error message
-