UNPKG

@applitools/eyes-playwright

Version:
127 lines (126 loc) 6.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTestResults = void 0; const jszip_1 = __importDefault(require("jszip")); const statusUtils_js_1 = require("../management/statusUtils.js"); const playwrightStatusUpdater_js_1 = require("../management/playwrightStatusUpdater.js"); const reportDataManager_js_1 = require("../management/reportDataManager.js"); const log_js_1 = __importDefault(require("../core/log.js")); const logger = (0, log_js_1.default)('[Eyes Data Parser]'); async function getTestResults() { var _a; // Step 1: Get Playwright report data from window object const reportData = window.playwrightReportBase64; const playwrightReportBase64 = typeof reportData === 'string' ? reportData : (_a = reportData === null || reportData === void 0 ? void 0 : reportData.textContent) !== null && _a !== void 0 ? _a : ''; if (!playwrightReportBase64) { logger.error('[Eyes Data Parser] No playwrightReportBase64 found in window'); return { testsFiles: {}, report: { files: [], stats: { expected: 0, unexpected: 0, flaky: 0, skipped: 0 } }, eyesTestResult: {}, sessionsByBatchId: {}, }; } const base64Data = playwrightReportBase64.replace('data:application/zip;base64,', ''); // Step 2: Unzip the report data const zip = new jszip_1.default(); await zip.loadAsync(base64Data, { base64: true }); // Step 3: Extract all test files (except report.json) const resultsByTestFile = await Promise.all(Object.keys(zip.files) .filter(fileName => fileName !== 'report.json') .map(async (fileName) => { const file = zip.files[fileName]; const content = await file.async('text'); return { [fileName]: JSON.parse(content) }; })); const testsFiles = Object.assign({}, ...resultsByTestFile); // Step 4: Extract report.json const reportFileObj = zip.file('report.json'); if (!reportFileObj) { throw new Error('[Eyes Data Parser] report.json not found in ZIP'); } const reportFile = await reportFileObj.async('text'); const report = JSON.parse(reportFile); // Step 5: Merge Eyes data from window.__testResultsMap const { eyesTestResult, sessionsByBatchId } = mergeEyesData(testsFiles, report); // Step 6: Re-zip and update window.playwrightReportBase64 with modified test statuses // This is critical! The test statuses have been modified in memory by updatePlaywrightTestStatus // We need to re-zip the data so Playwright's UI sees the updated statuses await (0, reportDataManager_js_1.refreshReportData)(testsFiles, report); return { testsFiles, report, eyesTestResult, sessionsByBatchId, }; } exports.getTestResults = getTestResults; /** * Merge Eyes data from window.__testResultsMap with Playwright tests */ function mergeEyesData(testsFiles, report) { const sessionsByBatchId = {}; const eyesTestResult = {}; // Check if Eyes data exists if (!window.__testResultsMap) { logger.warn('[Eyes Data Parser] No window.__testResultsMap found - no Eyes data to merge'); return { eyesTestResult, sessionsByBatchId }; } // Iterate through all test files Object.values(testsFiles).forEach(testFile => { // Find file in report for status updates const fileInReport = report.files.find(file => file.fileId === testFile.fileId); if (!fileInReport) return; testFile.tests.forEach(test => { const eyesResults = []; const eyesStatuses = []; // Look up Eyes results for each test retry test.results.forEach((_result, index) => { var _a; const lookupKey = `${test.testId}--${index}`; const eyesResultsForRetry = (_a = window.__testResultsMap) === null || _a === void 0 ? void 0 : _a[lookupKey]; if (eyesResultsForRetry) { // Get status for this retry const status = (0, statusUtils_js_1.getStatus)(eyesResultsForRetry); eyesStatuses.push(status.status); eyesResultsForRetry.forEach(eyesResult => { // Attach retry number to Eyes result eyesResult.playwrightRetry = _result.retry || 0; // Add to results array eyesResults.push(eyesResult); // Build sessionsByBatchId for polling if (!sessionsByBatchId[eyesResult.batchId]) { sessionsByBatchId[eyesResult.batchId] = { sessions: [], apiKey: eyesResult.eyesServer.apiKey, eyesServerUrl: eyesResult.eyesServer.eyesServerUrl, }; } sessionsByBatchId[eyesResult.batchId].sessions.push({ id: eyesResult.id, accessToken: eyesResult.secretToken, }); }); } else { // No Eyes results for this retry eyesStatuses.push(null); } }); // If test has Eyes results, add to eyesTestResult if (eyesResults.length > 0) { // Update Playwright test status based on Eyes results (0, playwrightStatusUpdater_js_1.updatePlaywrightTestStatus)(test, fileInReport, report.stats, eyesStatuses, true); eyesTestResult[test.testId] = { ...test, eyesResults, }; } }); }); return { eyesTestResult, sessionsByBatchId }; }