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')?cy.visit('https://example.com') cy.get('button#submit').click() cy.screenshot('after-submit')
cy.screenshot() captures by default and the file format it uses.The cy.screenshot() command takes a screenshot of the viewport by default. The name given ('after-submit') is used as the filename with a .png extension. It does not limit the screenshot to a specific element unless chained with .get() or .within().
cy.screenshot('homepage'). Which assertion code is correct in Cypress?cy.screenshot('homepage') // Which assertion below correctly verifies the screenshot file exists?
The screenshot is saved as a file on disk. To verify its existence, cy.readFile() is used to read the file, and should('exist') confirms the file is present. cy.get() is for DOM elements, so option A is invalid. Option A uses be.visible which applies to DOM elements, not files. Option A tries to assert on the command itself, which is incorrect.
cy.get('div.content').screenshot()It throws an error:
Cannot read property 'screenshot' of undefined. What is the most likely cause?cy.get('div.content').screenshot()
cy.get() finds no matching elements.If cy.get() finds no matching elements, it retries but eventually yields an empty set. Calling screenshot() on an empty or undefined element causes the error. The screenshot() command is valid on elements and cy. The selector syntax is correct. The filename argument is optional.
capture: 'viewport' in cy.screenshot()?capture: 'viewport' in the options of cy.screenshot() do?cy.screenshot('view', { capture: 'viewport' })
The capture: 'viewport' option tells Cypress to take a screenshot of only the visible area of the page (the viewport). The default is also 'viewport'; capture: 'fullPage' captures the entire page. It does not limit to a single element or disable the screenshot.
cy.screenshot() commands manually. Which configuration setting enables this behavior?The correct way is to set screenshotOnRunFailure: true in the cypress.config.js file under the e2e configuration. This tells Cypress to automatically capture screenshots on test failures. The other options are invalid commands or config keys.