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

  1. In your NodeJS based framework, add the following function:
  2. Copy

    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;
      }

  3. 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:

    Copy

    Conditions

    a. responseJSON.resources.length > 0
    b. responseJSON.metadata.processingStatus == "PROCESSING_COMPLETE"

Example usage of above method

The following example uses a command line-based JS tool to download the Test Summary report in PDF format.

Copy
Command Line tool
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:

  1. Create a new file as cli_report_summary.js.
  2. Copy the above code and paste it into the cli_report_summary.js file.
  3. Open a terminal window and navigate to the folder in which the cli_report_summary.js file is saved.
  4. Run following command:

    Copy
    Command to run cli tool
    node cli_report_summary.js
  5. The tool will prompt for required parameter:

  6. 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:

Related articles