What if your tests could snap perfect pictures of your app automatically, catching bugs you might miss?
Why Screenshot capture (cy.screenshot) in Cypress? - Purpose & Use Cases
Imagine you are testing a website manually and want to keep a record of how it looks at different steps. You take screenshots by pressing keys or using separate tools, switching between apps, and saving files one by one.
This manual way is slow and distracting. You might forget to take a screenshot at the right moment or save it in the wrong folder. It's easy to lose track, and comparing screenshots later becomes a big headache.
Using cy.screenshot() in Cypress lets you capture screenshots automatically during your tests. It saves images exactly when you want, with organized names and folders, without leaving your test flow.
Press Print Screen key -> Open Paint -> Paste -> Save file -> Repeat for each stepcy.visit('/page') cy.get('button').click() cy.screenshot('after-click')
It enables fast, reliable visual records of your app's state during tests, making bugs easier to spot and share.
When testing a shopping cart, you can automatically capture screenshots after adding items, so you have proof of the cart's content and layout at each step.
Manual screenshots are slow and error-prone.
cy.screenshot() automates capturing images during tests.
This helps track UI changes and find bugs faster.
Practice
cy.screenshot() in Cypress tests?Solution
Step 1: Understand the function of
This command takes a screenshot of the app at the moment it is called during the test.cy.screenshot()Step 2: Compare with other options
Options B, C, and D describe actions unrelated to capturing images.Final Answer:
To capture a picture of the app's current state during a test -> Option AQuick Check:
Screenshot = Capture app image [OK]
- Confusing screenshot with pausing tests
- Thinking it clears cache
- Assuming it simulates clicks
Solution
Step 1: Recall the correct method call syntax
In Cypress, methods are called with parentheses and arguments inside, likecy.screenshot('filename').Step 2: Check each option's syntax
cy.screenshot('homepage') uses correct function call syntax. Options A, C, and D use invalid JavaScript syntax for method calls.Final Answer:
cy.screenshot('homepage') -> Option CQuick Check:
Method call uses parentheses [OK]
- Using assignment (=) instead of function call
- Using colon (:) instead of parentheses
- Using brackets [] incorrectly
cy.get('button.submit').click();
cy.screenshot('after-click');What will happen when this test runs?
Solution
Step 1: Analyze the code sequence
The code first clicks the button with class 'submit', then callscy.screenshot('after-click')to take a screenshot with that name.Step 2: Understand the effect of
The screenshot is taken after the click, so the file will be named 'after-click'.cy.screenshot()Final Answer:
The test clicks the submit button and saves a screenshot named 'after-click'. -> Option DQuick Check:
Click then screenshot with name [OK]
- Thinking screenshot happens before click
- Believing screenshot requires no arguments
- Assuming no screenshot is taken
cy.screenshot('login-page', { capture: 'viewport' });But the screenshot file is not saved. What is the likely problem?
Solution
Step 1: Check the
Cypress supports 'viewport', 'runner', and 'fullPage' as valid values forcaptureoption valuescapture. 'viewport' is valid.Step 2: Review other options
File names can include dashes.cy.screenshotdoes not require chaining aftercy.get(). So these are not issues.Final Answer:
Thecaptureoption 'viewport' is valid; the problem is elsewhere. -> Option BQuick Check:
Valid options mean issue is not in syntax [OK]
- Assuming 'viewport' is invalid
- Thinking file names cannot have dashes
- Believing chaining is mandatory
.profile-pic and save it as 'profile.png'. Which code snippet correctly does this?Solution
Step 1: Understand element screenshot syntax
To screenshot a specific element, first select it withcy.get(), then call.screenshot()on that element. The argument is the file name without extension.Step 2: Evaluate each option
cy.get('.profile-pic').screenshot('profile.png.png'); uses file name with extension, which is incorrect; Cypress adds extension automatically. cy.screenshot('.profile-pic', { capture: 'element', fileName: 'profile.png' }); uses invalid syntax;cy.screenshotdoes not accept selector as first argument. cy.get('.profile-pic').screenshot('profile'); correctly selects element and calls.screenshot('profile'). cy.screenshot('profile.png', { element: '.profile-pic' }); uses invalid options.Final Answer:
cy.get('.profile-pic').screenshot('profile'); -> Option AQuick Check:
Element screenshot = get element then .screenshot(name) [OK]
- Passing file extension in screenshot name
- Calling cy.screenshot with selector argument
- Using invalid options for element capture
Ready to prove it?
Pass the test and this pattern moves unknown
