UNPKG

@applitools/eyes-playwright

Version:
116 lines (115 loc) 4.8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InternalData = void 0; const os = __importStar(require("os")); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const playwrightPath = require.resolve('playwright'); const HtmlReporter = require(path.join(path.dirname(playwrightPath), '/lib/reporters/html')).default; const pluginsFile = require.resolve('../../dist/fixture/reportRenderer.js'); const styleFile = require.resolve('./reporterStyle.css'); const createInjectedScript = (testResultsMap) => ` <script>window.__icons = { visualTest: \`${fs.readFileSync(require.resolve('./images/visual-text.svg'), 'utf-8')}\`, link: \`${fs.readFileSync(require.resolve('./images/link.svg'), 'utf-8')}\`, }</script> <script> ${fs.readFileSync(pluginsFile, 'utf-8')} window.__testResultsMap = ${JSON.stringify(testResultsMap)}; window.__initEyesReport(); </script> <style type="text/css">${fs.readFileSync(styleFile, 'utf-8')}</style> `; class EyesReporter extends HtmlReporter { constructor(options) { super(options); this.internalIds = []; } onTestEnd(test, result) { var _a; (_a = super.onTestEnd) === null || _a === void 0 ? void 0 : _a.call(this, test, result); const index = result.attachments.findIndex(a => a.name === 'internalId'); if (index > -1) { const internalId = result.attachments[index].body.toString(); this.internalIds.push(internalId); this.removeInternalIdAttachment(result); } } removeInternalIdAttachment(result) { var _a; const index = result.attachments ? result.attachments.findIndex(a => a.name === 'internalId') : -1; if (index > -1) (_a = result.attachments) === null || _a === void 0 ? void 0 : _a.splice(index, 1); result.steps.forEach(step => this.removeInternalIdAttachment(step)); } async onEnd(result) { await super.onEnd(result); const testResultsMap = {}; for (const internalId of this.internalIds) { const content = await exports.InternalData.consume(internalId); if (content) { testResultsMap[content.key] = content.data; } } try { const filePath = path.join(this._outputFolder, 'index.html'); fs.appendFileSync(filePath, createInjectedScript(testResultsMap)); } catch (e) { // eslint-disable-next-line no-console console.error('Error modifying index.html:', e); } } } exports.default = EyesReporter; exports.InternalData = { async write({ testInfo, data }) { const internalId = testInfo.attachments.find(a => a.name === 'internalId').body.toString(); const key = `${testInfo.testId}--${testInfo.retry}`; this.ensureFolder(); await fs.promises.writeFile(this.getPathForInternalId(internalId), JSON.stringify({ key, data })); }, async consume(internalId) { const filepath = this.getPathForInternalId(internalId); try { const content = await fs.promises.readFile(filepath, 'utf-8').then(JSON.parse); await fs.promises.unlink(filepath); return content; } catch (err) { // TODO no eyes results for test - this is ok, could be that no visual tests occurred for this playwright test } }, ensureFolder() { const folderPath = path.join(os.tmpdir(), 'eyes-report-files'); if (!fs.existsSync(folderPath)) { fs.mkdirSync(folderPath); } }, getPathForInternalId(internalId) { return path.join(os.tmpdir(), 'eyes-report-files', internalId); }, };