From f7dae2211a5270c33f2245022f927d768dd5b021 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Fri, 13 Dec 2024 11:29:35 +0100 Subject: [PATCH] fix(stepByStepReport): no records html is generated when running with run-workers --- lib/plugin/stepByStepReport.js | 45 +++++++++++++++++++++++++++++++++- package.json | 4 +-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/plugin/stepByStepReport.js b/lib/plugin/stepByStepReport.js index 21b96410a..852bf329b 100644 --- a/lib/plugin/stepByStepReport.js +++ b/lib/plugin/stepByStepReport.js @@ -4,6 +4,7 @@ const figures = require('figures') const fs = require('fs') const mkdirp = require('mkdirp') const path = require('path') +const cheerio = require('cheerio') const Container = require('../container') const recorder = require('../recorder') @@ -133,7 +134,49 @@ module.exports = function (config) { event.dispatcher.on(event.all.result, () => { if (Object.keys(recordedTests).length === 0 || !Object.keys(slides).length) return + generateRecordsHtml(recordedTests) + }) + + event.dispatcher.on(event.workers.result, async () => { + await recorder.add(() => { + const recordedTests = getRecordFoldersWithDetails(reportDir) + generateRecordsHtml(recordedTests) + }); + }); + + function getRecordFoldersWithDetails(dirPath) { + let results = {}; + try { + const items = fs.readdirSync(dirPath, { withFileTypes: true }); + + items.forEach(item => { + if (item.isDirectory() && item.name.startsWith('record_')) { + const recordFolderPath = path.join(dirPath, item.name); + const indexPath = path.join(recordFolderPath, 'index.html'); + + let name = ''; + if (fs.existsSync(indexPath)) { + try { + const htmlContent = fs.readFileSync(indexPath, 'utf-8'); + const $ = cheerio.load(htmlContent); + name = $('.navbar-brand').text().trim(); + } catch (err) { + console.error(`Error reading index.html in ${recordFolderPath}:`, err.message); + } + } + + results[name || 'Unkown'] = `${item.name}/index.html` + } + }); + } catch (err) { + console.error(`Error reading directory ${dirPath}:`, err.message); + } + + return results; + } + + function generateRecordsHtml(recordedTests) { let links = '' for (const link in recordedTests) { @@ -150,7 +193,7 @@ module.exports = function (config) { output.print( `${figures.circleFilled} Step-by-step preview: ${colors.white.bold(`file://${reportDir}/records.html`)}`, ) - }) + } async function persistStep(step) { if (stepNum === -1) return // Ignore steps from BeforeSuite function diff --git a/package.json b/package.json index e161753ef..cb79bfd6a 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "chai-match-pattern": "1.3.0", "chai-string": "1.5.0", "chalk": "4.1.2", + "cheerio": "^1.0.0", "commander": "11.1.0", "cross-spawn": "7.0.5", "css-to-xpath": "0.1.0", @@ -134,7 +135,6 @@ "apollo-server-express": "3.13.0", "chai-as-promised": "7.1.2", "chai-subset": "1.6.0", - "cheerio": "^1.0.0", "contributor-faces": "1.1.0", "documentation": "14.0.3", "electron": "33.2.1", @@ -184,4 +184,4 @@ "strict": false } } -} \ No newline at end of file +}