Bird
Raised Fist0
Cypresstesting~20 mins

Screenshot capture (cy.screenshot) in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Screenshot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the result of this Cypress screenshot command?
Consider the following Cypress test code snippet:
cy.visit('https://example.com')
cy.get('button#submit').click()
cy.screenshot('after-submit')

What will Cypress do when it runs cy.screenshot('after-submit')?
Cypress
cy.visit('https://example.com')
cy.get('button#submit').click()
cy.screenshot('after-submit')
AThrow an error because the screenshot name must not contain dashes.
BTake a screenshot only of the button with id 'submit' and save it as 'after-submit.png'.
CTake a screenshot and save it as 'after-submit.jpg' in the default screenshots folder.
DTake a screenshot of the viewport and save it as 'after-submit.png' in the default screenshots folder.
Attempts:
2 left
💡 Hint
Think about what cy.screenshot() captures by default and the file format it uses.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies a screenshot was taken?
You want to check that a screenshot file named 'homepage.png' exists after running cy.screenshot('homepage'). Which assertion code is correct in Cypress?
Cypress
cy.screenshot('homepage')
// Which assertion below correctly verifies the screenshot file exists?
Acy.readFile('cypress/screenshots/homepage.png').should('exist')
Bcy.readFile('cypress/screenshots/homepage.png').should('be.visible')
Ccy.get('cypress/screenshots/homepage.png').should('exist')
Dcy.screenshot('homepage').should('exist')
Attempts:
2 left
💡 Hint
Remember that screenshots are saved as files, not DOM elements.
🔧 Debug
advanced
2:00remaining
Why does this screenshot command fail with an error?
Look at this Cypress code:
cy.get('div.content').screenshot()

It throws an error: Cannot read property 'screenshot' of undefined. What is the most likely cause?
Cypress
cy.get('div.content').screenshot()
AThe <code>screenshot()</code> command is not valid on elements, only on <code>cy</code> itself.
BThe <code>cy.get()</code> command returned no elements, so <code>screenshot()</code> is called on undefined.
CThe selector <code>'div.content'</code> is invalid syntax causing the error.
DThe screenshot command requires a filename argument and fails without it.
Attempts:
2 left
💡 Hint
Think about what happens if cy.get() finds no matching elements.
🧠 Conceptual
advanced
2:00remaining
What is the effect of using capture: 'viewport' in cy.screenshot()?
In Cypress, what does setting capture: 'viewport' in the options of cy.screenshot() do?
Cypress
cy.screenshot('view', { capture: 'viewport' })
AIt captures only the element that triggered the screenshot command.
BIt captures the entire page including parts outside the visible area.
CIt captures only the visible part of the page currently shown in the browser window.
DIt disables the screenshot and logs a message instead.
Attempts:
2 left
💡 Hint
Think about the difference between viewport and full page screenshots.
❓ framework
expert
2:00remaining
How to configure Cypress to automatically take screenshots on test failure?
You want Cypress to automatically take a screenshot whenever a test fails, without adding cy.screenshot() commands manually. Which configuration setting enables this behavior?
ASet <code>"screenshotOnRunFailure": true</code> in <code>cypress.config.js</code> under <code>e2e</code> settings.
BAdd <code>cy.screenshotOnFail()</code> in every test's <code>afterEach</code> hook.
CSet <code>"autoScreenshot": true</code> in <code>cypress.json</code> file.
DUse <code>cy.screenshot({ onFail: true })</code> inside each test.
Attempts:
2 left
💡 Hint
Check Cypress official config options for screenshots on failure.

Practice

(1/5)
1. What is the primary purpose of using cy.screenshot() in Cypress tests?
easy
A. To capture a picture of the app's current state during a test
B. To pause the test execution
C. To clear the browser cache
D. To simulate user clicks

Solution

  1. Step 1: Understand the function of cy.screenshot()

    This command takes a screenshot of the app at the moment it is called during the test.
  2. Step 2: Compare with other options

    Options B, C, and D describe actions unrelated to capturing images.
  3. Final Answer:

    To capture a picture of the app's current state during a test -> Option A
  4. Quick Check:

    Screenshot = Capture app image [OK]
Hint: Screenshots = pictures of app state during tests [OK]
Common Mistakes:
  • Confusing screenshot with pausing tests
  • Thinking it clears cache
  • Assuming it simulates clicks
2. Which of the following is the correct syntax to take a screenshot with a custom file name in Cypress?
easy
A. cy.screenshot = 'homepage'
B. cy.screenshot: 'homepage'
C. cy.screenshot('homepage')
D. cy.screenshot['homepage']

Solution

  1. Step 1: Recall the correct method call syntax

    In Cypress, methods are called with parentheses and arguments inside, like cy.screenshot('filename').
  2. 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.
  3. Final Answer:

    cy.screenshot('homepage') -> Option C
  4. Quick Check:

    Method call uses parentheses [OK]
Hint: Use parentheses to pass file name in cy.screenshot() [OK]
Common Mistakes:
  • Using assignment (=) instead of function call
  • Using colon (:) instead of parentheses
  • Using brackets [] incorrectly
3. Consider this Cypress code snippet:
cy.get('button.submit').click();
cy.screenshot('after-click');

What will happen when this test runs?
medium
A. The test fails because cy.screenshot needs no arguments.
B. The test clicks the submit button but does not take any screenshot.
C. The test takes a screenshot before clicking the button.
D. The test clicks the submit button and saves a screenshot named 'after-click'.

Solution

  1. Step 1: Analyze the code sequence

    The code first clicks the button with class 'submit', then calls cy.screenshot('after-click') to take a screenshot with that name.
  2. Step 2: Understand the effect of cy.screenshot()

    The screenshot is taken after the click, so the file will be named 'after-click'.
  3. Final Answer:

    The test clicks the submit button and saves a screenshot named 'after-click'. -> Option D
  4. Quick Check:

    Click then screenshot with name [OK]
Hint: Commands run in order; screenshot after click saves named image [OK]
Common Mistakes:
  • Thinking screenshot happens before click
  • Believing screenshot requires no arguments
  • Assuming no screenshot is taken
4. You wrote this code:
cy.screenshot('login-page', { capture: 'viewport' });

But the screenshot file is not saved. What is the likely problem?
medium
A. The capture option value is incorrect; it should be 'runner'.
B. The capture option 'viewport' is valid; the problem is elsewhere.
C. The cy.screenshot command must be chained after cy.get().
D. The file name must not have a dash ('-').

Solution

  1. Step 1: Check the capture option values

    Cypress supports 'viewport', 'runner', and 'fullPage' as valid values for capture. 'viewport' is valid.
  2. Step 2: Review other options

    File names can include dashes. cy.screenshot does not require chaining after cy.get(). So these are not issues.
  3. Final Answer:

    The capture option 'viewport' is valid; the problem is elsewhere. -> Option B
  4. Quick Check:

    Valid options mean issue is not in syntax [OK]
Hint: Check option values; 'viewport' is valid for capture [OK]
Common Mistakes:
  • Assuming 'viewport' is invalid
  • Thinking file names cannot have dashes
  • Believing chaining is mandatory
5. You want to take a screenshot only of a specific element with class .profile-pic and save it as 'profile.png'. Which code snippet correctly does this?
hard
A. cy.get('.profile-pic').screenshot('profile');
B. cy.screenshot('.profile-pic', { capture: 'element', fileName: 'profile.png' });
C. cy.get('.profile-pic').screenshot('profile.png.png');
D. cy.screenshot('profile.png', { element: '.profile-pic' });

Solution

  1. Step 1: Understand element screenshot syntax

    To screenshot a specific element, first select it with cy.get(), then call .screenshot() on that element. The argument is the file name without extension.
  2. 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.screenshot does 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.
  3. Final Answer:

    cy.get('.profile-pic').screenshot('profile'); -> Option A
  4. Quick Check:

    Element screenshot = get element then .screenshot(name) [OK]
Hint: Select element first, then call .screenshot with name (no extension) [OK]
Common Mistakes:
  • Passing file extension in screenshot name
  • Calling cy.screenshot with selector argument
  • Using invalid options for element capture
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard