UNPKG

@argos-ci/cypress

Version:

Cypress SDK for visual testing with Argos.

149 lines (148 loc) 4.4 kB
import "cypress-wait-until"; import { getGlobalScript, resolveViewport } from "@argos-ci/browser"; import { getMetadataPath, getScreenshotName, validateThreshold } from "@argos-ci/util/browser"; //#region package.json var version = "7.0.14"; //#endregion //#region src/shared.ts /** * Prefix to identify Argos screenshots. */ const NAME_PREFIX = "argos/"; //#endregion //#region src/support.ts function injectArgos() { cy.window({ log: false }).then((window) => { if (typeof window.__ARGOS__ !== "undefined") return; window.eval(getGlobalScript()); }); } /** * Get the stabilization context from the options. */ function getStabilizationContext(options) { const { argosCSS, viewports } = options; return { fullPage: !options.capture || options.capture === "fullPage", argosCSS, viewports, options: options.stabilize }; } /** * Run before taking all screenshots. */ function beforeAll(options) { const context = getStabilizationContext(options); cy.window({ log: false }).then((window) => window.__ARGOS__.beforeAll(context)); return () => { cy.window({ log: false }).then((window) => window.__ARGOS__.afterAll()); }; } /** * Run before taking each screenshot. */ function beforeEach(options) { const context = getStabilizationContext(options); cy.window({ log: false }).then((window) => window.__ARGOS__.beforeEach(context)); return () => { cy.window({ log: false }).then((window) => window.__ARGOS__.afterEach()); }; } function getRetries(value) { if (typeof value !== "number" || !Number.isFinite(value)) return 0; const retries = Math.floor(value); return retries < 0 ? 0 : retries; } /** * Wait for the UI to be ready before taking the screenshot. */ function waitForReadiness(options) { const context = getStabilizationContext(options); cy.waitUntil(() => cy.window({ log: false }).then((window) => { if (window.__ARGOS__.waitFor(context)) return true; window.__ARGOS__.getWaitFailureExplanations(context).forEach((reason) => { cy.log(`[argos] stability: ${reason}`); }); return false; })); } Cypress.Commands.add("argosScreenshot", { prevSubject: [ "optional", "element", "window", "document" ] }, (subject, name, options = {}) => { const { viewports, argosCSS: _argosCSS, tag, ...cypressOptions } = options; if (!name) throw new Error("The `name` argument is required."); Cypress.log({ name: "argosScreenshot", displayName: `Argos Screenshot`, message: name }); injectArgos(); const afterAll = beforeAll(options); function stabilizeAndScreenshot(name) { waitForReadiness(options); const afterEach = beforeEach(options); waitForReadiness(options); const ref = {}; cy.wrap(subject).screenshot(`${NAME_PREFIX}${name}`, { blackout: ["[data-visual-test=\"blackout\"]"].concat(options.blackout || []), onAfterScreenshot: (_$el, props) => { ref.props = props; }, ...cypressOptions }); cy.window({ log: false }).then((window) => { const mediaType = window.__ARGOS__.getMediaType(); const colorScheme = window.__ARGOS__.getColorScheme(); const metadata = { url: window.location.href, viewport: { width: window.innerWidth, height: window.innerHeight }, colorScheme, mediaType, test: { title: Cypress.currentTest.title, titlePath: Cypress.currentTest.titlePath, retry: Cypress.currentRetry, retries: getRetries(cy.state("runnable")._retries) }, browser: { name: Cypress.browser.name, version: Cypress.browser.version }, automationLibrary: { name: "cypress", version: Cypress.version }, sdk: { name: "@argos-ci/cypress", version } }; metadata.transient = {}; if (tag) metadata.tags = Array.isArray(tag) ? tag : [tag]; if (options.threshold !== void 0) { validateThreshold(options.threshold); metadata.transient.threshold = options.threshold; } cy.writeFile(getMetadataPath(ref.props.path), JSON.stringify(metadata)); }); afterEach(); } if (viewports) { for (const viewport of viewports) { const viewportSize = resolveViewport(viewport); cy.viewport(viewportSize.width, viewportSize.height); stabilizeAndScreenshot(getScreenshotName(name, { viewportWidth: viewportSize.width })); } cy.viewport(Cypress.config("viewportWidth"), Cypress.config("viewportHeight")); } else stabilizeAndScreenshot(name); afterAll(); }); //#endregion export {};