-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerateReport.js
131 lines (107 loc) · 4.64 KB
/
generateReport.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
let report;
let puppeteer;
// Check if 'multiple-cucumber-html-reporter' is installed
try {
require.resolve('multiple-cucumber-html-reporter');
report = require('multiple-cucumber-html-reporter');
console.log('multiple-cucumber-html-reporter is already installed.');
} catch (e) {
console.log('Installing multiple-cucumber-html-reporter...');
execSync('npm install multiple-cucumber-html-reporter', { stdio: 'inherit' });
report = require('multiple-cucumber-html-reporter'); // Require it after installation
}
// Check if 'puppeteer' is installed
try {
require.resolve('puppeteer');
puppeteer = require('puppeteer');
console.log('Puppeteer is already installed.');
} catch (e) {
console.log('Installing Puppeteer...');
execSync('npm install puppeteer', { stdio: 'inherit' });
puppeteer = require('puppeteer'); // Require it after installation
}
// Set the output directory for the JSON files and report
const jsonDir = 'C:\\Users\\labuser\\Desktop\\AutoEventsReports\\label=AutoEvents\\reports\\combinedJSONs';
const outputDir = 'htmlReport';
const tempDir = 'tempJsonFiles';
// Create the output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
console.log(`Created output directory at: ${outputDir}`);
}
// Create a temporary directory for filtered JSON files
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
console.log(`Created temporary directory at: ${tempDir}`);
}
// Get the date argument from command line arguments
const dateArg = process.argv[2]; // This should be passed when running the script
async function generateHtmlReport() {
let combinedJsonFiles = [];
try {
// Filter JSON files that contain the date in their name
combinedJsonFiles = fs.readdirSync(jsonDir)
.filter(file => file.endsWith('.json') && file.includes(dateArg));
// Debug output to check the filtered files
console.log('Filtered JSON files:', combinedJsonFiles);
if (combinedJsonFiles.length === 0) {
console.error(`No JSON files found in the output directory for date: ${dateArg}.`);
process.exit(1); // Fail the build
}
// Copy filtered JSON files to the temporary directory
combinedJsonFiles.forEach(file => {
const oldPath = path.join(jsonDir, file);
const newPath = path.join(tempDir, file);
fs.copyFileSync(oldPath, newPath);
console.log(`Copied file: ${file} to temporary directory.`);
});
// Generate the report using the JSON files in the temporary directory
report.generate({
jsonDir: tempDir, // Specify the temporary directory containing JSON files
reportPath: outputDir, // Output path for the report
reportName: "Auto Events Report",
pageTitle: "Auto Events Report",
pageFooter: '<div style="text-align:center;"><p>Contact us for any support: <b>Grp-ccwt-e2e-support</b></p></div>',
customMetadata: true,
openReportInBrowser: false,
saveReport: true,
});
console.log(`HTML report generated successfully at: ${outputDir}`);
// Take a screenshot of the generated report
await takeScreenshot(outputDir);
// Delete the original JSON files from the jsonDir
// combinedJsonFiles.forEach(file => {
// const originalPath = path.join(jsonDir, file);
// fs.unlinkSync(originalPath);
// console.log(`Deleted original file: ${originalPath}`);
// });
} catch (err) {
console.error(`Error generating HTML report: ${err.message}`);
process.exit(1); // Fail the build on error
} finally {
fs.readdirSync(tempDir).forEach(file => {
fs.unlinkSync(path.join(tempDir, file)); // Delete each file in the temporary directory
});
fs.rmdirSync(tempDir); // Remove the temporary directory
console.log(`Cleaned up temporary directory: ${tempDir}`);
}
}
// Function to take a screenshot of the HTML report
async function takeScreenshot(reportDir) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Load the generated report (index.html)
const reportPath = `file://${path.join(__dirname, reportDir, 'index.html')}`;
await page.goto(reportPath);
await new Promise(resolve => setTimeout(resolve, 3000));
// Take a screenshot and save it in the same directory as the HTML report
const screenshotPath = path.join(reportDir, 'Auto_Events_Report.png');
await page.screenshot({ path: screenshotPath, fullPage: true });
console.log(`Screenshot saved at: ${screenshotPath}`);
await browser.close();
}
// Execute the report generation function
generateHtmlReport();