Guest User

writeToFile.js

a guest
Jun 8th, 2021
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const newman = require('newman'); // require newman in your project
  2. const fs = require('fs');
  3.  
  4. // call newman.run to pass `options` object and wait for callback
  5. newman.run({
  6.     collection: require('./JSON-Order_collection.json'),
  7.     environment: require('./EnvironmentJSON.json'),
  8.     reporters: 'cli'
  9. }).on('beforeRequest', (error, data) => {
  10.     if (error) {
  11.         console.log(error);
  12.         return;
  13.     }
  14.  
  15.     if (data.request.body) {
  16.         const requestName = data.item.name.replace(/[^a-z0-9]/gi, '-');
  17.         const randomString = Math.random().toString(36).substring(7);
  18.         const fileName = `request-${requestName}-${randomString}.txt`;
  19.         const content = data.request.body.raw;
  20.        
  21.         fs.writeFile(fileName, content, function (error) {
  22.             if (error) {
  23.                  console.error(error);
  24.             }
  25.          });        
  26.     }
  27. })
  28. .on('request', (error, data) => {
  29.     if (error) {
  30.         console.log(error);
  31.         return;
  32.     }
  33.  
  34.     const requestName = data.item.name.replace(/[^a-z0-9]/gi, '-');
  35.     const randomString = Math.random().toString(36).substring(7);
  36.     const fileName = `response-${requestName}-${randomString}.txt`;
  37.     const content = data.response.stream.toString();
  38.    
  39.     fs.writeFile(fileName, content, function (error) {
  40.         if (error) {
  41.              console.error(error);
  42.         }
  43.      });
  44.  
  45.      /// TODO: all global error handling
  46. });
Advertisement
Add Comment
Please, Sign In to add comment