UNPKG

e2ed

Version:

E2E testing framework over Playwright

104 lines (103 loc) 5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toMatchScreenshot = void 0; const node_crypto_1 = require("node:crypto"); const promises_1 = require("node:fs/promises"); 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 getScreenshotMeta_1 = require("./getScreenshotMeta"); 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, writeScreenshot } = (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 = { actualScreenshotId: undefined, actualScreenshotUrl: undefined, diffScreenshotId: undefined, diffScreenshotUrl: undefined, expectedScreenshotId, expectedScreenshotUrl: undefined, }; (0, object_1.setReadonlyProperty)(context, 'additionalLogFields', additionalLogFields); const meta = (0, getScreenshotMeta_1.getScreenshotMeta)({ actualValue, description, expectedScreenshotId, options }); let expectedScreenshotFound = false; if (expectedScreenshotId) { additionalLogFields.expectedScreenshotUrl = getScreenshotUrlById(expectedScreenshotId); const expectedScreenshot = await readScreenshot(expectedScreenshotId, meta); if (expectedScreenshot !== undefined) { expectedScreenshotFound = true; 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) { 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 actualScreenshot = await (0, promises_1.readFile)(actualScreenshotPath); const actualScreenshotId = await writeScreenshot(actualScreenshot, meta); additionalLogFields.actualScreenshotId = actualScreenshotId; additionalLogFields.actualScreenshotUrl = getScreenshotUrlById(actualScreenshotId); const diffScreenshot = await (0, promises_1.readFile)(diffScreenshotPath); const diffScreenshotId = await writeScreenshot(diffScreenshot, { ...meta, actual: actualScreenshotId, }); additionalLogFields.diffScreenshotId = diffScreenshotId; additionalLogFields.diffScreenshotUrl = getScreenshotUrlById(diffScreenshotId); } catch (secondError) { throw new error_1.E2edError(errorMessage, { secondError }); } throw new error_1.E2edError(errorMessage); } if (expectedScreenshotFound) { return; } try { const actualScreenshot = await (0, promises_1.readFile)(screenshotPath); const actualScreenshotId = await writeScreenshot(actualScreenshot, meta); additionalLogFields.actualScreenshotId = actualScreenshotId; additionalLogFields.actualScreenshotUrl = getScreenshotUrlById(actualScreenshotId); } catch (secondError) { throw new error_1.E2edError(message, { secondError }); } throw new error_1.E2edError(message); }; exports.toMatchScreenshot = toMatchScreenshot;