UNPKG

e2ed

Version:

E2E testing framework over Playwright

105 lines (104 loc) 4.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toMatchScreenshot = void 0; const node_crypto_1 = require("node:crypto"); const node_path_1 = require("node:path"); const configurator_1 = require("../../configurator"); const internal_1 = require("../../constants/internal"); const outputDirectoryName_1 = require("../../context/outputDirectoryName"); const config_1 = require("../config"); const error_1 = require("../error"); const fs_1 = require("../fs"); const object_1 = require("../object"); const screenshot_1 = require("../screenshot"); const getEmptyAdditionalLogFields_1 = require("./getEmptyAdditionalLogFields"); const getScreenshotMeta_1 = require("./getScreenshotMeta"); const writeScreenshotFromPath_1 = require("./writeScreenshotFromPath"); const test_1 = require("@playwright/test"); /** * Checks that the selector screenshot matches the one specified by `expectedScreenshotId`. * @internal */ // eslint-disable-next-line complexity, max-statements const toMatchScreenshot = async (context, expectedScreenshotId, options) => { const actualValue = context.actualValue; const { description } = context; const { getScreenshotUrlById, readScreenshot } = (0, config_1.getFullPackConfig)().matchScreenshot; const assertId = (0, node_crypto_1.randomUUID)(); const screenshotFileName = `${assertId}.png`; const screenshotPath = (0, node_path_1.join)(internal_1.EXPECTED_SCREENSHOTS_DIRECTORY_PATH, screenshotFileName); const additionalLogFields = (0, getEmptyAdditionalLogFields_1.getEmptyAdditionalLogFields)({ expectedScreenshotId }); (0, object_1.setReadonlyProperty)(context, 'additionalLogFields', additionalLogFields); const meta = (0, getScreenshotMeta_1.getScreenshotMeta)({ actualValue, description, expectedScreenshotId, options }); let expectedScreenshotFound = false; if (expectedScreenshotId) { additionalLogFields.expected.url = getScreenshotUrlById(expectedScreenshotId); const expectedScreenshot = await readScreenshot(expectedScreenshotId, meta); if (expectedScreenshot !== undefined) { expectedScreenshotFound = true; additionalLogFields.expected.dimensions = (0, screenshot_1.getDimensionsString)((0, screenshot_1.getPngDimensions)(expectedScreenshot)); if (!configurator_1.isLocalRun) { await (0, fs_1.writeFile)(screenshotPath, expectedScreenshot); } } } const message = expectedScreenshotId ? `Cannot read expected screenshot ${expectedScreenshotId}` : internal_1.SCREENSHOT_NOT_SPECIFIED_ERROR_MESSAGE; if (configurator_1.isLocalRun) { if (expectedScreenshotFound) { additionalLogFields.isLocalRun = 'The assertion completed successfully without comparing screenshots because this is a local run (screenshots are taken only when run in docker)'; return; } throw new error_1.E2edError(message); } const { mask = [], ...restOptions } = options; try { const playwrightLocator = actualValue.getPlaywrightLocator(); await (0, test_1.expect)(playwrightLocator, description).toHaveScreenshot(screenshotFileName, { mask: mask.map((selector) => selector.getPlaywrightLocator()), ...restOptions, }); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); try { const output = (0, node_path_1.join)(internal_1.INTERNAL_REPORTS_DIRECTORY_PATH, (0, outputDirectoryName_1.getOutputDirectoryName)()); const actualScreenshotPath = (0, node_path_1.join)(output, `${assertId}-actual.png`); const diffScreenshotPath = (0, node_path_1.join)(output, `${assertId}-diff.png`); const actualScreenshotId = await (0, writeScreenshotFromPath_1.writeScreenshotFromPath)({ additionalLogFields, meta, path: actualScreenshotPath, type: 'actual', }); await (0, writeScreenshotFromPath_1.writeScreenshotFromPath)({ additionalLogFields, meta: { ...meta, actual: actualScreenshotId }, path: diffScreenshotPath, type: 'diff', }); } catch (secondError) { throw new error_1.E2edError(errorMessage, { secondError }); } throw new error_1.E2edError(errorMessage); } if (expectedScreenshotFound) { return; } try { await (0, writeScreenshotFromPath_1.writeScreenshotFromPath)({ additionalLogFields, meta, path: screenshotPath, type: 'actual', }); } catch (secondError) { throw new error_1.E2edError(message, { secondError }); } throw new error_1.E2edError(message); }; exports.toMatchScreenshot = toMatchScreenshot;