Sometimes you want to change how a built-in Cypress command works to fit your test better. Overwriting commands lets you do that easily.
Overwriting existing commands in 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.
cy.click() runs, then does the normal click.Cypress.Commands.overwrite('click', (originalFn, subject, options) => { console.log('Click command is called') return originalFn(subject, options) })
cy.visit() so if you give a path, it adds the base URL automatically.Cypress.Commands.overwrite('visit', (originalFn, url, options) => { if (!url.startsWith('http')) { url = 'https://example.com' + url } return originalFn(url, options) })
This test overwrites the type command to always type uppercase letters. It logs the text before typing.
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') }) })
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.
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
Solution
Step 1: Understand command overwriting
Overwriting commands means changing how existing Cypress commands behave.Step 2: Identify the purpose
This lets you add features like logging or fix issues in commands.Final Answer:
To change or extend the behavior of built-in Cypress commands -> Option AQuick Check:
Overwriting = Change behavior [OK]
- Thinking overwriting deletes commands
- Confusing overwriting with creating new commands
- Assuming overwriting speeds up tests automatically
click command in Cypress?Solution
Step 1: Recall Cypress overwrite syntax
UseCypress.Commands.overwritewith the command name and a function receivingoriginalFn.Step 2: Check parameters and call
The function must calloriginalFn(subject, options)to keep original behavior.Final Answer:
Cypress.Commands.overwrite('click', (originalFn, subject, options) => { return originalFn(subject, options) }) -> Option DQuick Check:
Overwrite syntax = Cypress.Commands.overwrite [OK]
- Using Cypress.Commands.add instead of overwrite
- Wrong method name like override
- Missing originalFn parameter
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?Solution
Step 1: Analyze the overwrite function
The overwrite logs the text being typed, then calls the originaltypecommand with all parameters.Step 2: Understand the effect on
Whency.typecy.get('input').type('hello')runs, it logs the message and types 'hello' into the input.Final Answer:
It types 'hello' and logs 'Typing: hello' in the test runner -> Option AQuick Check:
Overwrite logs then calls original = logs and types [OK]
- Assuming logging prevents typing
- Thinking parameters are incorrect
- Believing it throws an error
Cypress.Commands.overwrite('click', (subject, options) => {
cy.log('Clicking element')
return subject.click(options)
})Solution
Step 1: Check overwrite function parameters
Overwrites must receiveoriginalFnas the first parameter to call the original command.Step 2: Identify missing parameter
This code only hassubjectandoptions, missingoriginalFn.Final Answer:
It misses the originalFn parameter required for overwriting -> Option BQuick Check:
Overwrite needs originalFn first param [OK]
- Omitting originalFn parameter
- Confusing add with overwrite
- Thinking cy.log is disallowed
visit command to add a console log only when the URL contains 'admin'. Which overwrite code correctly implements this?Solution
Step 1: Use correct overwrite signature
Overwrite must haveoriginalFnas first parameter, thenurlandoptions.Step 2: Call originalFn with correct argument order
CalloriginalFn(url, options)to keep original behavior.Step 3: Add conditional console log
Check ifurl.includes('admin')and log message accordingly.Final Answer:
Cypress.Commands.overwrite('visit', (originalFn, url, options) => { if (url.includes('admin')) { console.log('Admin page visited') } return originalFn(url, options) }) -> Option CQuick Check:
Overwrite with originalFn and correct args [OK]
- Swapping url and options in call
- Using add instead of overwrite
- Omitting originalFn parameter
Ready to prove it?
Pass the test and this pattern moves unknown
