Untitled
import './commonUtils'; const fs = require('fs'); const path = require('path'); const config = require('../../config'); class EventReporter { /** * Register and save xapi results for the test * @param {string} testname name of the test * @param {object} results test result including pass/fail/expected/actual data */ async registerGameEvents(testname, results) { if (global.xApiResults === undefined) global.xApiResults = {}; global.xApiResults[testname] = {}; global.xApiResults[testname].results = []; for (const result of results) { const testResultDataTemplate = `var data = ${JSON.stringify( result, )};\nconst testCaseName = '${ result.testCaseName }';\nconst passResultsCount = ${ result.passResultsCount };\nconst failResultsCount = ${result.failResultsCount};`; const tempResult = {}; tempResult.resultDataTemplate = testResultDataTemplate; tempResult.resultData = result; global.xApiResults[testname].results.push(tempResult); } } /** * Generate test case report by plugging in data into xApiResultReport template * @param {*} testname name of the test */ async generateTestCaseReport(testname) { const xApiDataDir = config.xApiData_root; const testNameData = []; const { results } = global.xApiResults[testname]; const htmlDir = path.join(config.xApiReport_root, testname); const indexFile = path.join(htmlDir, 'index.html'); const testCaseNameData = []; const passResultsCountData = []; const failResultsCountData = []; let isAllTestCasesPassing = true; let index = 1; if (!fs.existsSync(htmlDir)) await fs.mkdirSync(htmlDir, { recursive: true }); for (const result of results) { const { resultData, resultDataTemplate } = result; const htmlFile = path.join(htmlDir, `xApiResultReport-${index}.html`); await fs.readFile( path.join( config.testData_root, 'events', 'xApiResultReportTemplate.html', ), 'utf-8', (_err, data) => { const formattedData = data.replace('%s', resultDataTemplate); fs.writeFileSync(htmlFile, formattedData); }, ); index += 1; const { testCaseName, passResultsCount, failResultsCount } = resultData; testCaseNameData.push(testCaseName); passResultsCountData.push(passResultsCount); failResultsCountData.push(failResultsCount); isAllTestCasesPassing = failResultsCount !== 0 ? false : isAllTestCasesPassing; } testNameData.push(testname); if (!fs.existsSync(xApiDataDir)) await fs.mkdirSync(xApiDataDir, { recursive: true }); await fs.readFile( path.join(config.testData_root, 'events', 'gameIndexTemplate.html'), 'utf-8', (err, data) => { if (err) throw err.message; let formattedData = data.replace( '%s', testCaseNameData.map((x) => `'${x}'`).toString(), ); formattedData = formattedData.replace( '%s', passResultsCountData.toString(), ); formattedData = formattedData.replace( '%s', failResultsCountData.toString(), ); fs.writeFileSync(indexFile, formattedData); }, ); const testDataFile = path.join(xApiDataDir, `${testname}.js`); // this is needed for generating suite report const testData = `export const testname = '${testname}'; export const isAllTestCasesPassing = ${isAllTestCasesPassing};`; fs.writeFileSync(testDataFile, testData); } /** * Generate test suite report by plugging in data into suiteIndexTemplate */ async generateTestSuiteReport() { const suiteIndexFile = path.join(config.xApiReport_root, 'index.html'); const xApiDataDir = config.xApiData_root; const testDataFiles = await fs.readdirSync(xApiDataDir); const testnames = []; const isAllTestCasesPassings = []; for (const file of testDataFiles) { const testDataFile = path.join(xApiDataDir, file); const { testname, isAllTestCasesPassing } = await import(testDataFile); testnames.push(testname); isAllTestCasesPassings.push(isAllTestCasesPassing); } await fs.readFile( path.join(config.testData_root, 'events', 'suiteIndexTemplate.html'), 'utf-8', (err, data) => { if (err) throw err.message; let formattedData = data.replace( '%s', testnames.map((x) => `'${x}'`).toString(), ); formattedData = formattedData.replace( '%s', isAllTestCasesPassings.toString(), ); fs.writeFileSync(suiteIndexFile, formattedData); }, ); } } module.exports = new EventReporter();
Leave a Comment