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:
-
browseris the instance of the webdriver. For WDIO, this will be the browser object. -
job_nameis the CI job name -
job_numberis the CI job number (needs to be a number) -
job_branchis the branch name (optional) -
project_nameis the name of the project (optional) -
project_versionis the version of the project (optional) -
tagsis the array of strings representing the global test tags (optional) -
customFieldsis 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:
-
reportingClientis the reporting client object (initialized as described in Step 2) -
testNameis the string representing the name of current test -
tagsis the array of strings representing the local tags applied to the test -
customFieldsis 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:
-
reportingClientis 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:
-
reportingClientis the instance of the reporting client created by the function in Step 2 -
errMessageis the string representing the error message
-