Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple cucumber report footer from landing page and individual modules #88

Merged
merged 14 commits into from
Jul 17, 2024
11 changes: 11 additions & 0 deletions cypress/fixtures/customReportData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"customFooter": {
"PageFooter": "Custom footer for main page",
"Account": "<div><p><b>Custom for Account pages</b></p></div>",
"Device": "<div><p><b>Custom for Device pages</b></p></div>"
},
"customMetadata": {
"Account": "SetupAccount1</br>SetupAccount2",
"Device": "SetupDevice1</br>SetupDevice2"
}
}
7 changes: 7 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,14 @@ module.exports = async (on, config) => {
jsonReport = readDataFromFile(filePath + fileName);
}
const reportProperties = {};
let customReportData;
try {
customReportData = require('../fixtures/external/objects/customReportData.json');
} catch (error) {
customReportData = require('../fixtures/customReportData.json');
}
reportProperties.isCombinedTestRun = process.env.CYPRESS_isCombinedTestRun;
reportProperties.customReportData = customReportData;
// Add the report to the reportObj
if (reportType === CONSTANTS.CUCUMBER) {
reportObj.cucumberReport = jsonReport;
Expand Down
65 changes: 64 additions & 1 deletion cypress/plugins/localReportGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const logger = require('../support/Logger')('localReportGenerator.js');
const rename = util.promisify(fs.rename);
const readdir = util.promisify(fs.readdir);
const mkdir = util.promisify(fs.mkdir);
const readFileAsync = util.promisify(fs.readFile);
const writeFileAsync = util.promisify(fs.writeFile);

/**
* Generates local reports for Mochawesome and Cucumber based on the provided report data.
Expand Down Expand Up @@ -70,16 +72,28 @@ async function generateLocalReport(reportObj, jobId) {
if (reportObj.cucumberReport && reportObj.cucumberReportFilePath) {
// Move cucumber json to a separate directory and get the path
const cucumberDir = await filterCucumberJson(reportObj.cucumberReportFilePath);

let customReportData;
try {
customReportData = require('../fixtures/external/objects/customReportData.json');
} catch (error) {
customReportData = require('../fixtures/customReportData.json');
}
// Configure cucumber report options
reportEnv.jsonDir = cucumberDir;
reportEnv.reportPath = `./reports/${jobId}/cucumber-html-report`;
const featuresDir = `./reports/${jobId}/cucumber-html-report/features`;
if (customReportData.customFooter)
reportEnv.pageFooter = customReportData.customFooter.PageFooter;

// Generate the cucumber report
await cucumberReportGenerator.generate(reportEnv);

// Remove tags from the generated cucumber report
removeTagsFromCukeHtml(reportEnv.reportPath + '/index.html');
if (customReportData.customFooter)
await processFeaturesFiles(featuresDir, customReportData.customFooter, 'customFooter');
if (customReportData.customMetadata)
await processFeaturesFiles(featuresDir, customReportData.customMetadata, 'customMetadata');

logger.info(
`A local report has been generated and can be accessed at ./reports/${jobId}/cucumber-html-report/index.html`,
Expand Down Expand Up @@ -165,4 +179,53 @@ function removeTagsFromCukeHtml(htmlReportPath) {
}
}

async function updateCustomData(filePath, newCustomData, flag) {
const data = await readFileAsync(filePath, 'utf8');
let updatedData;
if (flag.includes('customFooter')) {
const regex = /<div id="pagefooter">.*?<\/div>/s;
updatedData = data.replace(regex, newCustomData);
} else if (flag.includes('customMetadata')) {
let regex = /<h2>Metadata<\/h2>/;

// Replace the found pattern with the new header content
updatedData = data.replace(regex, '<h2>Setup & Metadata</h2>');
regex = /(<ul class="quick-list">)([\s\S]*?)(<\/ul>)/;
// Prepare the "Setup" section and HTML content from newCustomData
const setupContent = `<li><span class="meta-data-title">Setup</span><ul class="prereq-list">`;
const metadataContent = Object.values(newCustomData).join('');
const finalContent = setupContent + metadataContent + '</ul></li>';

updatedData = data.replace(regex, `$1$2\n${finalContent}$3`);
}
await writeFileAsync(filePath, updatedData, 'utf8');
}

// Process each footer in html files
async function processFeaturesFiles(reportDir, customData, customFlag) {
try {
const files = await readdir(reportDir);

for (const file of files) {
const data = await getCustomData(file, customData);
if (data) {
const filePath = path.join(reportDir, file);
await updateCustomData(filePath, data, customFlag);
}
}
} catch (err) {
console.error('Error reading report directory:', err);
}
}

// Function to get the custom footer based on filename
async function getCustomData(fileName, customData) {
for (const key in customData) {
if (fileName.includes(`${key}.html`)) {
return customData[key];
}
}
return null;
}

module.exports = { generateLocalReport };
Loading