Skip to main content
Cypress App

After Screenshot Event

After a screenshot is taken, you can get details about the screenshot via the after:screenshot node event. Cypress calls this event when it takes a screenshot with cy.screenshot() or as a result of a test failure. Cypress calls the event after it writes the screenshot image to disk.

Your handler records those details, manipulates the image as needed, and returns the updated details about the image.

Syntax​

caution

⚠️ This code is part of the setupNodeEvents function and thus executes in the Node environment. You cannot call Cypress or cy commands in this function, but you do have the direct access to the file system and the rest of the operating system.

const { defineConfig } = require('cypress')

module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:screenshot', (details) => {
/* ... */
})
},
},
})

details (object)

An object describing the screenshot that was taken, with the following properties:

PropertyTypeDescription
blackoutarrayThe blackout argument passed in via cy.screenshot()
dimensionsobjectThe width and height of the image in pixels (example: { width: 100, height: 50 })
durationnumberDuration taking and saving the screenshot (in milliseconds).
multipartbooleanWhether the screenshot was stitched together from multiple screenshot images.
namestring(Optional) The fileName argument passed in via cy.screenshot()
pathstringThe absolute path to the image.
pixelRationumber(Optional) The ratio of screenshot pixels to the browser's displayed pixels.
scaledbooleanWhether the application under test was scaled to fit into the browser viewport. May be scale argument passed in via cy.screenshot()
sizenumberThe size of the image file (in bytes).
specNamestringThe name of the specfile where the screenshot was taken.
takenAtstringThe date and time the screenshot was taken. ISO 8601 format in UTC (example: '2020-03-09T07:30:37.686Z')
testAttemptIndexnumberThe index of the test attempt that took the screenshot, starting at 0. With test retries, each retry increases the index by one.
testFailureboolean(Optional) true when Cypress took the screenshot automatically because a test failed. Cypress leaves it out for screenshots taken with cy.screenshot().

Usage​

Modify screenshot details​

Using setupNodeEvents you can tap into the after:screenshot event.

If you change the path, size, or dimensions of the image, you need to update the new values so that the test results report the details correctly. Cypress ignores any other properties besides path, size, and dimensions.

You can return an object or a promise that resolves to an object from the callback function. Any type of returned value other than an object is ignored. The object can contain the following properties:

  • path: The absolute path to the current location of the image
  • size: The size of the current image file (in bytes)
  • dimensions: The width and height of the current image in pixels (as an object with the shape { width: 100, height: 50 })

Cypress merges the properties into the screenshot details and passes them to the onAfterScreenshot callback (if defined with Cypress.Screenshot.defaults() and/or cy.screenshot()).

Modify screenshot path​

If you move the location of the screenshot image, specify the new path of the image.

const { defineConfig } = require('cypress')
const fs = require('fs')

module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:screenshot', (details) => {
console.log(details) // print all details to terminal

const newPath = '/new/path/to/screenshot.png'

return new Promise((resolve, reject) => {
// fs.rename moves the file to the existing directory 'new/path/to'
// and renames the image to 'screenshot.png'
fs.rename(details.path, newPath, (err) => {
if (err) return reject(err)

// because we renamed and moved the image, resolve with the new path
// so it is accurate in the test results
resolve({ path: newPath })
})
})
})
},
},
})

History​

VersionChanges
16.3.0When you register the event more than once, every handler runs in registration order, each receiving the result of the one before it, instead of only the last one.

See also​