0
0
Cypresstesting~15 mins

Dual commands in Cypress - Build an Automation Script

Choose your learning style9 modes available
Verify login and logout functionality using dual commands
Preconditions (2)
Step 1: Enter 'testuser' in the username input field with id 'username'
Step 2: Enter 'Password123!' in the password input field with id 'password'
Step 3: Click the login button with id 'loginBtn'
Step 4: Verify that the URL changes to '/dashboard'
Step 5: Click the logout button with id 'logoutBtn'
Step 6: Verify that the URL changes back to '/login'
✅ Expected Result: User successfully logs in and is redirected to dashboard, then logs out and is redirected back to login page
Automation Requirements - Cypress
Assertions Needed:
URL should be '/dashboard' after login
URL should be '/login' after logout
Best Practices:
Use Cypress dual commands chaining for actions and assertions
Use clear and stable selectors (id selectors)
Avoid arbitrary waits; rely on Cypress automatic waits
Use descriptive test and step names
Automated Solution
Cypress
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.

Common Mistakes - 3 Pitfalls
Using multiple separate cy.get() commands without chaining
Using arbitrary waits like cy.wait(5000)
Using unstable selectors like XPath or complex CSS selectors
Bonus Challenge

Now add data-driven testing with 3 different sets of valid usernames and passwords

Show Hint