0
0
Cypresstesting~20 mins

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

Choose your learning style9 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.