Verify login and logout functionality using dual commands
Preconditions (2)
✅ Expected Result: User successfully logs in and is redirected to dashboard, then logs out and is redirected back to login page
describe('Login and Logout flow using dual commands', () => { it('should login and logout successfully', () => { cy.visit('/login') .get('#username').type('testuser') .get('#password').type('Password123!') .get('#loginBtn').click() .url().should('include', '/dashboard') .get('#logoutBtn').click() .url().should('include', '/login') }) })
This test uses Cypress dual commands chaining to perform actions and assertions in a smooth flow.
First, it visits the login page, then types the username and password using get().type(). Next, it clicks the login button and asserts the URL includes '/dashboard' to confirm successful login.
Then it clicks the logout button and asserts the URL includes '/login' to confirm successful logout.
Using chaining keeps the code clean and readable. Cypress automatically waits for elements to be ready, so no explicit waits are needed.
Now add data-driven testing with 3 different sets of valid usernames and passwords
dual commands in Cypress tests?submitBtn using a dual command in Cypress?cy.get(selector).click() finds the element and clicks it in one chain.find which is not a root command.cy.get('.input-field').type('Hello').should('have.value', 'Hello')type() types text into the element and returns the element for further chaining.should('have.value', 'Hello') checks if the input's value is 'Hello', which is correct after typing.cy.get('.checkbox').check('#checkbox1')check() command acts on the element found by get() and does not take any selector argument.check('#checkbox1') incorrectly passes a selector, causing an error.firstName and 'Doe' into input with id lastName in Cypress?Pass the test and this pattern moves unknown