4.2 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| screenshot | true |
Take a screenshot of the application under test with the Cypress Command Log.
Syntax
cy.screenshot()
cy.screenshot(fileName)
cy.screenshot(options)
cy.screenshot(fileName, options)
Usage
.screenshot() cannot be chained off any other cy commands, so should be chained off of cy for clarity.
{% fa fa-check-circle green %} Valid Usage
cy.screenshot()
Arguments
{% fa fa-angle-right %} fileName (String)
A name for the image file. By default the filename will be the title of the test.
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of cy.screenshot().
| Option | Default | Notes |
|---|---|---|
log |
true |
whether to display command in command log |
timeout |
responseTimeout |
Total time to wait for the automation server to process the command. |
Yields
cy.screenshot() yields null.
Timeout
cy.screenshot will wait up for the duration of responseTimeout for the automation server to process this command.
Examples
The screenshot will be stored in the cypress/screenshots folder by default.
You can change the directory where screenshots are saved in your configuration.
Screenshot
Take a screenshot
describe('my tests', function(){
it('takes a screenshot', function(){
cy.screenshot() // saved as 'cypress/sreenshots/my tests -- takes a screenshot.png'
})
})
Filename
Take a screenshot and save as specific filename
// screenshot will be saved as
// cypress/sreenshots/clickingOnNav.png
cy.screenshot('clickingOnNav')
Notes
Automatic screenshots on test failure
When running headlessly or in Continuous Integration, Cypress automatically takes a screenshot when a test fails. You can optionally turn this off by setting screenshotOnHeadlessFailure to false in your configuration.
Screenshots in CI
You can see screenshots taken during a CI run in the Cypress Dashboard without any extra work.
Alternatively, to see screenshots in the Circle CI UI, we automatically export screenshots as artifacts. This makes them available directly in their web UI.
If you're using Travis, you'll need to upload artifacts to an s3 bucket as per their uploading artifacts doc to see screenshots outside of the Cypress Dashboard.
Understanding when a screenshot happens
Taking a screenshot is an asynchronous action that takes around 100ms to complete. By the time the screenshot is taken, it's possible something in your application may have changed. It's important to realize that the screenshot may not reflect 100% of what your application looked like when the command was issued.
For example - say a command we wrote times outs: cy.get('#element'). This causes your test to fail. Cypress then takes a screenshot when the test fails, but it's possible something in your application changed within the 100ms timeframe. Hypothetically your app could render the element you were searching for. When this happens the screenshot may provide confusing results. It's unlikely, but theoretically possible.
Command Log
Take a screenshot with a specific filename
cy.screenshot('my-image')
The commands above will display in the command log as:
When clicking on screenshot within the command log, the console outputs the following: