Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Cypress SDK reference (@argos-ci/cypress)

Reference for @argos-ci/cypress: the cy.argosScreenshot command, the upload task, stabilization options, and viewports for Cypress visual tests.

Boost your visual testing capabilities by combining Argos with your Cypress tests.

While Cypress inherently provides screenshot functionality, the Argos Cypress integration enhances this by:

  • Ensuring all images are fully loaded.

  • Ensuring all fonts are rendered.

  • Confirming the absence of any aria-busy (loading) elements on the page.

  • Concealing scrollbars.

  • Obscuring text cursors or carets.

  • Providing CSS utilities to simplify content hiding.

  • Gives you visibility on test failures.

Get started

Please refer to our Quickstart guide to get started with Argos and Cypress.

Compatibility

  • Cypress 12 through 15.

  • Node.js 22 or later.

Set a Preview URL

Argos displays the URL of the page when a screenshot is taken, helping you understand the screenshot’s context in the Argos UI. If you run tests locally and deploy your pull requests (PRs) to a preview URL, you can link the two by setting the ARGOS_PREVIEW_BASE_URL environment variable or configuring the previewUrl option in the Cypress configuration.

Example Configuration

Setup individual Cypress events

Cypress only supports one handler per event. If you need to set up other handlers for the same event, you can call the individual functions provided by the SDK.

Animations

cy.argosScreenshot keeps Cypress's own handling of animations: the disableTimersAndAnimations option of cy.screenshot, true by default, stops JavaScript timers and CSS animations while the screenshot is taken. You can pass it to cy.argosScreenshot.

Argos stabilization also pauses, on their first frame, SVG animations that repeat forever (stabilize.pauseSvgAnimations) and animated GIFs (stabilize.pauseGifs).

API Overview

cy.argosScreenshot(name[, options])

To capture a single element, chain the command from it: cy.get(".post").argosScreenshot("post").

  • name: Unique name for the screenshot. Required.

  • options: Explore cy.screenshot command options for details.

  • options.padding: Space to capture around the element, in CSS pixels, to include shadows, focus rings or anything else overflowing its box. A number applies to every side, and an array of 1 to 4 numbers follows the CSS padding shorthand, such as [8, 16] for 8px above and below and 16px on each side. Only applies when the command is chained off an element: cy.get("[role=dialog]").argosScreenshot("dialog", { padding: 16 }). The captured area is clamped to the page. Near the edge of the page, cy.screenshot's own padding shifts the area back inside the page instead, which captures more on the opposite side.

  • options.viewports: Define specific viewports for capturing screenshots. More on viewports configuration.

  • options.argosCSS: Specific CSS applied during the screenshot process. More on injecting CSS

  • options.hide: Array of selectors of elements to hide. Hidden elements keep their space in the layout, like with data-visual-test="transparent". Selectors that match nothing are ignored. More on helpers.

  • options.remove: Array of selectors of elements to remove. Removed elements no longer take space in the layout, like with data-visual-test="removed". Selectors that match nothing are ignored. An element in both hide and remove is removed.

  • options.blackout: Array of selectors of elements to black out, from cy.screenshot. They are blacked out in addition to the elements with data-visual-test="blackout".

  • options.threshold: Sensitivity threshold between 0 and 1. The higher the threshold, the less sensitive the diff will be. Defaults to 0.5. More on how the threshold works.

  • options.baseName: Name, or list of names, to compare this screenshot against instead of its own name. Useful to compare a variant against an existing screenshot. More on fallback baselines.

  • options.stabilize: Wait for the UI to stabilize before taking the screenshot. Set to false to disable stabilization. Pass an object to customize the stabilization. Defaults to true.

  • options.stabilize.disableSpellCheck: Disable spell check before taking the screenshot. Defaults to true.

  • options.stabilize.fontAntialiasing: Force font antialiasing. Defaults to true.

  • options.stabilize.hideCarets: Hide text carets before taking the screenshot. Defaults to true.

  • options.stabilize.hideScrollbars: Hide scrollbars before taking the screenshot. Defaults to true.

  • options.stabilize.loadImageSrcset: Force the loading of images with srcset attributes when the viewport changes. Defaults to true.

  • options.stabilize.pauseGifs: Pause animated GIFs on their first frame so they don't capture a random frame on each run. Defaults to true. Flag GIFs served from extension-less URLs with data-image-type="gif" so they're detected too.

  • options.stabilize.pauseSvgAnimations: Pause SVG animations that repeat forever (repeatCount="indefinite" or repeatDur="indefinite"), such as spinners, on their first frame. Defaults to true.

  • options.stabilize.roundImageSize: Round image sizes to the nearest integer. Defaults to true.

  • options.stabilize.stabilizeSticky: Stabilize sticky and fixed elements by switching to position: absolute. Defaults to true.

  • options.stabilize.waitForAriaBusy: Wait for the aria-busy attribute to be removed from the document. Defaults to true.

  • options.stabilize.waitForFonts: Wait for fonts to be loaded. Defaults to true.

  • options.stabilize.waitForImages: Wait for images to be loaded. Defaults to true.

  • options.stabilize.waitForBackgroundImages: Wait for CSS background images (including ::before/::after) to load before taking the screenshot. Enabled by default, scoped to elements flagged with the data-visual-test-wait-bg-img attribute (and their descendants). Pass true to scan the whole document, { selector: string } to target a custom selector, or false to disable it. A failed image (e.g. a 404) is treated as loaded so it never blocks stabilization.

  • options.tag: Tag or array of tags to attach to the screenshot for filtering in Argos.

Helper Attributes for Visual Testing

For tailored visual testing, the data-visual-test attributes provide control over how elements appear in Argos screenshots. This can be especially useful for obscuring or modifying elements with dynamic content, like dates.

  • [data-visual-test="transparent"]: Renders the element transparent (opacity: 0), keeping its layout space.

  • [data-visual-test="removed"]: Removes the element from view (display: none).

  • [data-visual-test="blackout"]: Masks the element with a blackout effect.

  • [data-visual-test-no-radius]: Strips the border radius from the element.

Example: Using a helper attribute to hide a div from the captured screenshot:

Example: Using the hide, remove and blackout options, without changing the markup:

hide works like transparent, remove like removed, and blackout like the blackout helper. hide and remove ignore selectors that match nothing, and an element in both is removed. Argos restores the elements after the screenshot and applies hide and remove again for each viewport.

registerArgosTask(on, config[, options])

  • on: Cypress plugin events.

  • config: Cypress config.

  • options: All upload parameters.

  • options.uploadToArgos: Upload results and create a build on Argos, true by default.

argosAfterScreenshot(config, details[, options])

Cypress "after:screenshot" event handler.

  • config: Cypress config.

  • details: Screenshot details provided by Cypress.

  • options: All upload parameters.

  • options.uploadToArgos: Upload results and create a build on Argos, true by default.

argosAfterRun(config, results[, options])

Cypress "after:run" event handler.

  • config: Cypress config.

  • results: Run results provided by Cypress.

  • options: All upload parameters.

  • options.uploadToArgos: Upload results and create a build on Argos, true by default.

Troubleshooting

Error while importing @argos-ci/cypress/task in cypress.config.ts

To address the ts(1479) error when importing @argos-ci/cypress/task in your cypress.config.ts, you have two main strategies:

1

Update your tsconfig.json

Set moduleResolution to "Bundler". This method leverages TypeScript's support for newer module resolution strategies, aligning with Node.js's exports feature used by Argos to distribute optimized SDKs.

2

Suppress the error in cypress.config.ts

Add the line // @ts-expect-error moduleResolution right above the import statement. This tells TypeScript to expect an error at this line and ignore it, offering a quick workaround without adjusting your project's module resolution strategy.

The first option is a more comprehensive solution, dealing with the TypeScript bug through adopting the new moduleResolution: "Bundler" setting, which is designed for such cases. The second option is simpler and quicker but bypasses the issue rather than solving it at its core.

Viewports option not working

When running Cypress in headless mode, the Cypress.viewport command (used internally by @argos-ci/cypress) may not behave as expected. This is because headless browsers don’t render a visible viewport, which can result in incorrect or inconsistent screenshots.

To ensure a consistent viewport size, configure it via setupNodeEvents in your cypress.config.js. This approach sets the viewport before the browser launches, avoiding visual regressions.

Additional Resources

Last updated

Was this helpful?