Test Overview
This test adds a custom Cypress command to fill and submit a login form. It verifies that after submission, the user is redirected to the dashboard page.
This test adds a custom Cypress command to fill and submit a login form. It verifies that after submission, the user is redirected to the dashboard page.
Cypress.Commands.add('login', (username, password) => { cy.get('#username').type(username); cy.get('#password').type(password); cy.get('#login-button').click(); }); describe('Custom Command: login', () => { it('should login and redirect to dashboard', () => { cy.visit('/login'); cy.login('testuser', 'testpass'); cy.url().should('include', '/dashboard'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Cypress test runner is ready | — | PASS |
| 2 | Browser opens and navigates to '/login' | Login page is displayed with username, password fields and login button | — | PASS |
| 3 | Custom command 'login' is called with username 'testuser' and password 'testpass' | Username and password fields are empty | — | PASS |
| 4 | Find element '#username' and type 'testuser' | Username field contains 'testuser' | Verify username field value is 'testuser' | PASS |
| 5 | Find element '#password' and type 'testpass' | Password field contains 'testpass' | Verify password field value is 'testpass' | PASS |
| 6 | Find element '#login-button' and click | Login form submitted | — | PASS |
| 7 | Check that URL includes '/dashboard' | Browser navigated to dashboard page | URL contains '/dashboard' | PASS |
Cypress.Commands.add() in Cypress testing?login in Cypress?Cypress.Commands.add().cy.login() is called in a test? Cypress.Commands.add('login', (username, password) => {
cy.get('#user').type(username)
cy.get('#pass').type(password)
cy.get('#submit').click()
})
// In test
cy.login('user1', 'pass1') Cypress.Commands.add('clickButton', () => {
cy.get('#button')
cy.click()
})cy.get('#button') returns an element, but cy.click() is called separately, not chained.cy.get('#button').click().click() to cy.get() -> Option Aclick() to cy.get() [OK]fillForm that fills multiple fields and submits a form. Which approach correctly uses Cypress.Commands.add() to accept an object with field values and perform the actions?data with named properties is clear and flexible.data.name and data.email matches the object keys.Pass the test and this pattern moves unknown