NodeJS: Download the report summary PDF
Perfecto's Smart Reporting API provides an option to download test execution summary report in PDF format. For details, see PDF download after test execution.
Step-by-step guide
- In your NodeJS based framework, add the following function:
-
Call the above function after the test execution is complete.
To check whether the test execution is complete or not one can call test results API and check following properties:
CopyConditions
a. responseJSON.resources.length > 0
b. responseJSON.metadata.processingStatus == "PROCESSING_COMPLETE"
Download PDF
function downloadPDF(baseUrl,securityToken,jobName,jobNumber){
deferred = protractor.promise.defer();
stream = fs.createWriteStream("./report.pdf");
var options = {
"hostname":baseUrl,
"port":443,
"path":"/export/api/v1/test-executions/pdf?jobName%5B0%5D=" + jobName + "&jobNumber%5B0%5D=" + jobNumber,
"headers":{
"PERFECTO_AUTHORIZATION":securityToken
}
};
https.get(options, function(response,cb) {
response.pipe(stream);
stream.on('finish', function () {
stream.close(cb);
deferred.fulfill('Download completed');
});
}).on('error', function(e) {
deferred.reject(e + ' for downloading report ');
});
return deferred.promise;
}
Example usage of above method
The following example uses a command line-based JS tool to download the Test Summary report in PDF format.
const readline = require('readline');
var fs = require('fs');
const https= require('https');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Reading Input from User
rl.question('Please provide Cloud Url without protocol: ', (answer) => {
var cloudUrl;
var securityToken;
var jobName;
var jobNumber;
cloudUrl = answer;
if(cloudUrl==""){
console.log("Cloud Url can't be Empty!!");
rl.close();
process.exit(1);
}
rl.question('Please provide Cloud Security Token: ', (answer) => {
securityToken = answer;
if(securityToken==""){
console.log("Security token can't be Empty!!");
rl.close();
process.exit(1);
}
rl.question('Please provide Job Name: ', (answer) => {
jobName = answer;
if(jobName==""){
console.log("Job Name can't be Empty!!");
rl.close();
process.exit(1);
}
rl.question('Please provide Job Number: ', (answer) => {
jobNumber = answer;
if(jobNumber==""){
console.log("Job Number can't be Empty!!");
}else{
downloadPDF(cloudUrl,securityToken,jobName,jobNumber);
}
rl.close();
});
});
});
});
// Download pdf using Reportium API
function downloadPDF(baseUrl,securityToken,jobName,jobNumber){
stream = fs.createWriteStream("./report.pdf");
var options = {
"hostname":baseUrl,
"port":443,
"path":"/export/api/v1/test-executions/pdf?jobName%5B0%5D=" + jobName + "&jobNumber%5B0%5D=" + jobNumber,
"url":baseUrl,
"headers":{
"PERFECTO_AUTHORIZATION":securityToken
}
};
https.get(options, function(response,cb) {
response.pipe(stream);
stream.on('finish', function () {
stream.close(cb);
console.log("Download Completed. Please check current folder to find result.pdf");
});
}).on('error', function(e) {
console.log("Error Occured - " + e);
});
}
To use the above code:
- Create a new file as cli_report_summary.js.
- Copy the above code and paste it into the cli_report_summary.js file.
- Open a terminal window and navigate to the folder in which the cli_report_summary.js file is saved.
-
Run following command:
-
The tool will prompt for required parameter:
- After providing all the required information, the tool will generate the summary pdf in the correct directory of the script.
To download the report using alternative methods:
- Use Postman: Download execution report using REST API call
- Use cURL command: cURL Command Samples