0
0
Cypresstesting~10 mins

Cypress.Commands.add() - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - Cypress
Cypress
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');
  });
});
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsCypress test runner is ready-PASS
2Browser opens and navigates to '/login'Login page is displayed with username, password fields and login button-PASS
3Custom command 'login' is called with username 'testuser' and password 'testpass'Username and password fields are empty-PASS
4Find element '#username' and type 'testuser'Username field contains 'testuser'Verify username field value is 'testuser'PASS
5Find element '#password' and type 'testpass'Password field contains 'testpass'Verify password field value is 'testpass'PASS
6Find element '#login-button' and clickLogin form submitted-PASS
7Check that URL includes '/dashboard'Browser navigated to dashboard pageURL contains '/dashboard'PASS
Failure Scenario
Failing Condition: Login button element '#login-button' is missing or not clickable
Execution Trace Quiz - 3 Questions
Test your understanding
What does the custom command 'login' do in this test?
AOnly types the username
BTypes username and password, then clicks login button
COnly clicks the login button
DNavigates to the dashboard page directly
Key Result
Using Cypress.Commands.add() helps reuse common actions like login, making tests cleaner and easier to maintain.