0
0
Cypresstesting~10 mins

TypeScript support for custom commands in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test adds a custom Cypress command with TypeScript support and verifies it works correctly by checking the page title.

Test Code - Cypress
Cypress
/// <reference types="cypress" />

declare namespace Cypress {
  interface Chainable {
    login(username: string, password: string): Chainable<void>
  }
}

Cypress.Commands.add('login', (username, password) => {
  cy.get('#username').type(username)
  cy.get('#password').type(password)
  cy.get('#login-btn').click()
})

describe('Custom Command with TypeScript', () => {
  it('should login using custom command and verify title', () => {
    cy.visit('https://example.com/login')
    cy.login('user1', 'pass123')
    cy.title().should('include', 'Dashboard')
  })
})
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsCypress test runner initialized with TypeScript support-PASS
2Browser opens and navigates to https://example.com/loginLogin page is loaded with username, password fields and login button-PASS
3Finds element #username and types 'user1'Username input field contains 'user1'-PASS
4Finds element #password and types 'pass123'Password input field contains 'pass123'-PASS
5Finds element #login-btn and clicks itLogin form submitted, page navigates to dashboard-PASS
6Checks page title includes 'Dashboard'Page title is 'User Dashboard - Example'Title includes 'Dashboard'PASS
Failure Scenario
Failing Condition: Custom command 'login' fails to find input fields or login button
Execution Trace Quiz - 3 Questions
Test your understanding
What does the custom command 'login' do in this test?
AOnly types username
BTypes username and password then clicks login button
CClicks login button without typing
DNavigates to the dashboard page
Key Result
Defining custom commands with TypeScript interfaces improves code completion and type safety, making tests easier to write and maintain.