0
0
Cypresstesting~10 mins

Overwriting existing commands in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test overwrites the default Cypress click command to add a console log before clicking. It verifies that the overwritten command works correctly by clicking a button and checking the button's text.

Test Code - Cypress
Cypress
// Overwrite the click command to add a console log
Cypress.Commands.overwrite('click', (originalFn, subject, options) => {
  console.log('Custom click command called')
  return originalFn(subject, options)
})

describe('Overwrite click command test', () => {
  beforeEach(() => {
    cy.visit('https://example.cypress.io/commands/actions')
  })

  it('should use overwritten click command and verify button text', () => {
    cy.get('.action-btn').first().click()
    cy.get('.action-btn').first().should('have.text', 'Action')
  })
})
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsCypress test runner initialized-PASS
2Browser opens and navigates to https://example.cypress.io/commands/actionsPage with action buttons loadedPage loaded successfullyPASS
3Find the first element with class '.action-btn'Button element foundElement existsPASS
4Call overwritten click command on the buttonConsole logs 'Custom click command called', button is clickedClick action performedPASS
5Find the first '.action-btn' again and check its textButton element visible with textButton text is 'Action'PASS
6Test endsTest completed successfully-PASS
Failure Scenario
Failing Condition: The button element with class '.action-btn' is not found or click does not trigger correctly
Execution Trace Quiz - 3 Questions
Test your understanding
What does the overwritten click command do before clicking?
AChanges the button text
BPrevents the click action
CLogs a message to the console
DWaits for 5 seconds
Key Result
Overwriting commands in Cypress allows adding custom behavior while preserving original functionality, which helps in extending tests without breaking existing commands.