Capture screenshot after successful login
Preconditions (2)
✅ Expected Result: Screenshot file is saved with the dashboard page visible after login
describe('Login and capture screenshot', () => { it('should login and take a screenshot of the dashboard', () => { cy.visit('https://example.com/login'); cy.get('#email').type('user@example.com'); cy.get('#password').type('Password123!'); cy.get('#loginBtn').click(); cy.url().should('include', '/dashboard'); cy.screenshot('dashboard-page'); }); });
This test script uses Cypress to automate the login process and capture a screenshot.
First, cy.visit() opens the login page.
Then, cy.get() locates the email and password fields by their IDs and types the credentials.
The login button is clicked using cy.get() and .click().
We verify the URL contains '/dashboard' to confirm successful login using cy.url().should().
Finally, cy.screenshot() captures the current page screenshot named 'dashboard-page'.
All commands use Cypress's built-in waiting, so no explicit waits are needed.
Now add data-driven testing to login with 3 different sets of valid credentials and capture screenshots for each dashboard.
cy.screenshot() in Cypress tests?cy.screenshot()cy.screenshot('filename').cy.get('button.submit').click();
cy.screenshot('after-click');cy.screenshot('after-click') to take a screenshot with that name.cy.screenshot()cy.screenshot('login-page', { capture: 'viewport' });capture option valuescapture. 'viewport' is valid.cy.screenshot does not require chaining after cy.get(). So these are not issues.capture option 'viewport' is valid; the problem is elsewhere. -> Option B.profile-pic and save it as 'profile.png'. Which code snippet correctly does this?cy.get(), then call .screenshot() on that element. The argument is the file name without extension.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.Pass the test and this pattern moves unknown