Overwriting existing commands in Cypress - Build an Automation Script
/// <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.
Now add overwriting for the 'type' command to log the typed text before typing
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
