Bird
Raised Fist0
Cypresstesting~15 mins

Overwriting existing commands in Cypress - Build an Automation Script

Choose your learning style10 modes available
Overwrite Cypress 'click' command to log a message before clicking
Preconditions (2)
Step 1: Open the test file where Cypress commands can be overwritten
Step 2: Overwrite the existing 'click' command to log 'Click command is called' before performing the click
Step 3: Visit the web page with the button
Step 4: Use the overwritten 'click' command to click the button with id 'submit-btn'
Step 5: Verify that the button was clicked by checking a visible change or a URL change
✅ Expected Result: The console logs 'Click command is called' before the button is clicked, and the button click action is performed successfully
Automation Requirements - Cypress
Assertions Needed:
Verify the button click triggers expected behavior (e.g., URL change or visible text change)
Verify the overwritten command logs the message before clicking
Best Practices:
Use Cypress.Commands.overwrite to overwrite commands
Keep the original command functionality intact by calling the original command inside the overwrite
Use proper selectors (e.g., #submit-btn) for locating elements
Avoid hardcoding waits; rely on Cypress automatic waits
Automated Solution
Cypress
/// <reference types="cypress" />

// cypress/support/commands.js
Cypress.Commands.overwrite('click', (originalFn, subject, options) => {
  cy.log('Click command is called');
  return originalFn(subject, options);
});

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

  it('logs message before clicking the button', () => {
    // The button with id 'submit-btn' does not exist on example page,
    // so we use a button that triggers visible change: '#action-canvas'
    cy.get('#action-canvas').click();

    // Verify the canvas has been clicked by checking a CSS property change
    cy.get('#action-canvas').should('have.css', 'background-color');
  });
});

First, we overwrite the click command in cypress/support/commands.js using Cypress.Commands.overwrite. We add a cy.log call to print a message before calling the original click command to keep its behavior intact.

Then, in the test file overwrite_click_spec.js, we visit a sample page from Cypress examples that has interactive elements. We select a button-like element with id action-canvas and use the overwritten click command.

Finally, we assert that the element's CSS property changed after the click, confirming the click worked. The log message appears in Cypress Test Runner's Command Log, showing the overwrite worked.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not calling the original command inside the overwrite', 'why_bad': "This breaks the original command functionality, so the click won't happen.", 'correct_approach': 'Always call the original function passed as the first argument to keep the original behavior.'}
Using hardcoded waits like cy.wait(1000) instead of relying on Cypress automatic waits
Overwriting commands in test files instead of support/commands.js
Bonus Challenge

Now add overwriting for the 'type' command to log the typed text before typing

Show Hint

Practice

(1/5)
1. What is the main purpose of overwriting existing commands in Cypress?
easy
A. To change or extend the behavior of built-in Cypress commands
B. To delete commands from Cypress
C. To create new commands unrelated to existing ones
D. To speed up test execution by skipping commands

Solution

  1. Step 1: Understand command overwriting

    Overwriting commands means changing how existing Cypress commands behave.
  2. Step 2: Identify the purpose

    This lets you add features like logging or fix issues in commands.
  3. Final Answer:

    To change or extend the behavior of built-in Cypress commands -> Option A
  4. Quick Check:

    Overwriting = Change behavior [OK]
Hint: Overwriting means changing existing command behavior [OK]
Common Mistakes:
  • Thinking overwriting deletes commands
  • Confusing overwriting with creating new commands
  • Assuming overwriting speeds up tests automatically
2. Which of the following is the correct syntax to overwrite the click command in Cypress?
easy
A. Cypress.Commands.add('click', (subject, options) => { return subject.click(options) })
B. Cypress.Commands.override('click', (originalFn, subject) => { originalFn(subject) })
C. Cypress.overwrite('click', (subject, options) => { return subject.click(options) })
D. Cypress.Commands.overwrite('click', (originalFn, subject, options) => { return originalFn(subject, options) })

Solution

  1. Step 1: Recall Cypress overwrite syntax

    Use Cypress.Commands.overwrite with the command name and a function receiving originalFn.
  2. Step 2: Check parameters and call

    The function must call originalFn(subject, options) to keep original behavior.
  3. Final Answer:

    Cypress.Commands.overwrite('click', (originalFn, subject, options) => { return originalFn(subject, options) }) -> Option D
  4. Quick Check:

    Overwrite syntax = Cypress.Commands.overwrite [OK]
Hint: Use Cypress.Commands.overwrite with originalFn first param [OK]
Common Mistakes:
  • Using Cypress.Commands.add instead of overwrite
  • Wrong method name like override
  • Missing originalFn parameter
3. Given this overwrite code:
Cypress.Commands.overwrite('type', (originalFn, subject, text, options) => {
  cy.log(`Typing: ${text}`)
  return originalFn(subject, text, options)
})

What will happen when cy.get('input').type('hello') runs?
medium
A. It types 'hello' and logs 'Typing: hello' in the test runner
B. It types 'hello' but does not log anything
C. It throws an error because of wrong parameters
D. It logs 'Typing: hello' but does not type anything

Solution

  1. Step 1: Analyze the overwrite function

    The overwrite logs the text being typed, then calls the original type command with all parameters.
  2. Step 2: Understand the effect on cy.type

    When cy.get('input').type('hello') runs, it logs the message and types 'hello' into the input.
  3. Final Answer:

    It types 'hello' and logs 'Typing: hello' in the test runner -> Option A
  4. Quick Check:

    Overwrite logs then calls original = logs and types [OK]
Hint: Overwrite calls originalFn after logging to keep behavior [OK]
Common Mistakes:
  • Assuming logging prevents typing
  • Thinking parameters are incorrect
  • Believing it throws an error
4. What is wrong with this overwrite code?
Cypress.Commands.overwrite('click', (subject, options) => {
  cy.log('Clicking element')
  return subject.click(options)
})
medium
A. It should use Cypress.Commands.add instead of overwrite
B. It misses the originalFn parameter required for overwriting
C. It uses cy.log which is not allowed inside overwrites
D. It calls subject.click instead of originalFn

Solution

  1. Step 1: Check overwrite function parameters

    Overwrites must receive originalFn as the first parameter to call the original command.
  2. Step 2: Identify missing parameter

    This code only has subject and options, missing originalFn.
  3. Final Answer:

    It misses the originalFn parameter required for overwriting -> Option B
  4. Quick Check:

    Overwrite needs originalFn first param [OK]
Hint: Overwrite must have originalFn as first parameter [OK]
Common Mistakes:
  • Omitting originalFn parameter
  • Confusing add with overwrite
  • Thinking cy.log is disallowed
5. You want to overwrite the visit command to add a console log only when the URL contains 'admin'. Which overwrite code correctly implements this?
hard
A. Cypress.Commands.overwrite('visit', (url, options) => { if (url.includes('admin')) { console.log('Admin page visited') } return cy.visit(url, options) })
B. Cypress.Commands.overwrite('visit', (originalFn, url, options) => { if (url.includes('admin')) { console.log('Admin page visited') } return originalFn(options, url) })
C. Cypress.Commands.overwrite('visit', (originalFn, url, options) => { if (url.includes('admin')) { console.log('Admin page visited') } return originalFn(url, options) })
D. Cypress.Commands.add('visit', (url, options) => { if (url.includes('admin')) { console.log('Admin page visited') } cy.visit(url, options) })

Solution

  1. Step 1: Use correct overwrite signature

    Overwrite must have originalFn as first parameter, then url and options.
  2. Step 2: Call originalFn with correct argument order

    Call originalFn(url, options) to keep original behavior.
  3. Step 3: Add conditional console log

    Check if url.includes('admin') and log message accordingly.
  4. Final Answer:

    Cypress.Commands.overwrite('visit', (originalFn, url, options) => { if (url.includes('admin')) { console.log('Admin page visited') } return originalFn(url, options) }) -> Option C
  5. Quick Check:

    Overwrite with originalFn and correct args [OK]
Hint: Always call originalFn with correct parameters in overwrite [OK]
Common Mistakes:
  • Swapping url and options in call
  • Using add instead of overwrite
  • Omitting originalFn parameter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard