-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostmanToIcanloadJS.js
More file actions
60 lines (46 loc) · 2.43 KB
/
postmanToIcanloadJS.js
File metadata and controls
60 lines (46 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const fs = require('fs');
const path = require('path');
const icanloadjs = require('icanloadjs'); // Ganti dengan path yang sesuai ke skrip icanloadjs Anda
const convertPostmanToIcanloadJS = (collectionFile, environmentFile, icanloadjsScript) => {
// Load Postman environment file
const environmentData = JSON.parse(fs.readFileSync(environmentFile, 'utf8'));
// Extract base URL from the environment file with nullish check
const baseUrl = environmentData.values.find((v) => v.key === 'baseUrl')?.value;
// Load Postman collection file
const collectionData = JSON.parse(fs.readFileSync(collectionFile, 'utf8'));
// Extract request details from the collection file
const request = collectionData.item[0].request;
// Check if request has a body
const dataMode = request.body ? request.body.mode : null;
// Assuming the request body mode is specified and dataMode is not null
const data = dataMode === 'raw' ? request.body.raw : null; // Assuming raw body mode
// Generate the icanloadjs script content
const scriptContent = `
const icanloadjs = require('${icanloadjsScript}');
const method = '${request.method.toUpperCase()}';
const numRequests = 1;
const numVirtualUsers = 1;
const thresholds = {
maxFailedChecks: 1,
http_req_failed: 0.01, // 1% failure rate
http_req_duration: 200, // 95% of requests below 200ms
// ... other thresholds
};
const data = ${data ? JSON.stringify(data) : 'null'}; // Include data if available
icanloadjs.runIcan('${request.url.raw.replace('{{baseUrl}}', baseUrl)}', method, numRequests, numVirtualUsers, thresholds, data);
`;
// Save the generated script to a file
const outputPath = path.join(__dirname, 'icanloadScript.js');
fs.writeFileSync(outputPath, scriptContent, 'utf8');
console.log(`The icanloadjs script has been generated and saved to: ${outputPath}`);
};
// Command-line arguments
const [collectionFile, environmentFile, icanloadjsScript] = process.argv.slice(2);
// Check if required arguments are provided
if (!collectionFile || !environmentFile || !icanloadjsScript) {
console.error('Usage: node postmanToIcanloadJS.js <collectionFile> <environmentFile> <icanloadjsScript>');
process.exit(1);
}
// Run the conversion script
convertPostmanToIcanloadJS(collectionFile, environmentFile, icanloadjsScript);
// Example : node postmanToIcanloadJS.js postmantoicanload.postman_collection.json fakeAPI.postman_environment.json icanloadjs