Rita Basu-ghosh
Rita Basu-ghosh

Reputation: 51

What tool can I use to take a screenshot with cypress automation?

I have a cypress spec.js and I would like to take several screenshots after particular assertion of the whole page several times. I want the screenshot to be available on a folder or in the report.
I have mochawesome and Junit reporter. What is the best screenshot tool I can use?

Upvotes: 0

Views: 1044

Answers (1)

Alapan Das
Alapan Das

Reputation: 18649

You can use the cy.screenshot() command to take screenshots in cypress. You can read more about it from here.

After assertion you can do like this:

cy.get('selector').should('have.text', 'some text').screenshot()

You can also directly use the command without chaining into any previous commands like this:

cy.screenshot()

You can also add the screenshot name as well like this:

cy.get('selector')
  .should('have.text', 'some text')
  .screenshot('name') //will save screenshot with name.png

You can also save the screenshot as per your desired directory as well like this:

cy.get('selector')
  .should('have.text', 'some text')
  .screenshot('path to folder/name')

Now you can pass the capture options to let cypress know Which parts of the Test Runner to capture. This value is ignored for element screenshot captures.

cy.get('selector')
  .should('have.text', 'some text')
  .screenshot('name', {capture: 'fullPage'})
  • viewport - The application under test is captured in the current viewport.
  • fullPage - The application under test is captured in its entirety from top to bottom.
  • runner - The entire browser viewport, including the Cypress Command Log, is captured.

Upvotes: 1

Related Questions