Bird
Raised Fist0
Cypresstesting~5 mins

Overwriting existing commands in Cypress

Choose your learning style10 modes available
Introduction

Sometimes you want to change how a built-in Cypress command works to fit your test better. Overwriting commands lets you do that easily.

You want to add extra logging every time a command runs.
You need to change the default behavior of a command for your app.
You want to add custom checks before or after a command runs.
You want to reuse a command but with some tweaks.
You want to fix or improve a command without changing all your tests.
Syntax
Cypress
Cypress.Commands.overwrite('commandName', (originalFn, ...args) => {
  // your custom code here
  return originalFn(...args)
})

originalFn is the original command you are overwriting.

You must call originalFn(...args) to keep the original behavior unless you want to replace it fully.

Examples
This example adds a console log every time cy.click() runs, then does the normal click.
Cypress
Cypress.Commands.overwrite('click', (originalFn, subject, options) => {
  console.log('Click command is called')
  return originalFn(subject, options)
})
This changes cy.visit() so if you give a path, it adds the base URL automatically.
Cypress
Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
  if (!url.startsWith('http')) {
    url = 'https://example.com' + url
  }
  return originalFn(url, options)
})
Sample Program

This test overwrites the type command to always type uppercase letters. It logs the text before typing.

Cypress
Cypress.Commands.overwrite('type', (originalFn, subject, text, options) => {
  console.log(`Typing text: ${text}`)
  return originalFn(subject, text.toUpperCase(), options)
})

describe('Overwrite type command test', () => {
  it('types uppercase text', () => {
    cy.visit('https://example.cypress.io')
    cy.get('.search-input').type('hello')
  })
})
OutputSuccess
Important Notes

Always call the original function unless you want to fully replace the command.

Overwriting commands affects all tests, so use carefully.

Use descriptive console logs to help debug overwritten commands.

Summary

Overwriting commands lets you change how Cypress commands work.

You get the original command as a function to call inside your overwrite.

Use overwrites to add logging, change behavior, or fix commands.

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