Challenge - 5 Problems
Cypress Command Overwriter
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Cypress test after overwriting?
Consider the following Cypress command overwrite and test code. What will be the output in the test runner?
Cypress
Cypress.Commands.overwrite('click', (originalFn, subject, options) => { cy.log('Custom click called'); return originalFn(subject, options); }); describe('Overwrite click test', () => { it('should log custom message and click', () => { cy.visit('https://example.cypress.io') cy.get('a').first().click(); }); });
Attempts:
2 left
💡 Hint
Remember that the overwrite calls the original function after logging.
✗ Incorrect
The overwrite wraps the original 'click' command, logs a message, then calls the original click. This means the click still happens and the log appears once.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies the overwritten command was called?
You have overwritten the 'type' command to log a message. Which assertion below correctly checks that the log was called during a test?
Cypress
Cypress.Commands.overwrite('type', (originalFn, subject, text, options) => { cy.log('Custom type called'); return originalFn(subject, text, options); }); // Test code cy.get('input').type('hello');
Attempts:
2 left
💡 Hint
Think about how to check if a Cypress command like cy.log was called with a specific argument.
✗ Incorrect
Using cy.spy on cy.log allows checking if it was called with the expected message. The other options incorrectly check DOM elements or text.
🔧 Debug
advanced2:00remaining
Why does this overwritten command cause a test to hang?
Analyze the following overwrite code. Why does the test hang indefinitely when calling cy.get('button').click()?
Cypress
Cypress.Commands.overwrite('click', (originalFn, subject, options) => { cy.log('Click overwrite start'); cy.get('button').click(); return originalFn(subject, options); });
Attempts:
2 left
💡 Hint
Think about what happens when the overwritten command calls itself inside.
✗ Incorrect
Calling cy.get('button').click() inside the click overwrite calls the overwritten click again, causing infinite recursion and hanging the test.
🧠 Conceptual
advanced2:00remaining
What is the main benefit of overwriting Cypress commands?
Why would a tester choose to overwrite an existing Cypress command instead of creating a new custom command?
Attempts:
2 left
💡 Hint
Think about how overwriting affects all uses of a command.
✗ Incorrect
Overwriting allows changing how existing commands behave everywhere, so tests can keep using the same commands but with new behavior.
❓ framework
expert3:00remaining
How to properly overwrite 'visit' command to add a default timeout?
You want to overwrite Cypress's 'visit' command to add a default timeout of 10000ms if no timeout is specified. Which overwrite implementation is correct?
Cypress
Cypress.Commands.overwrite('visit', (originalFn, url, options) => { // Your code here });
Attempts:
2 left
💡 Hint
Remember that later properties in object spread override earlier ones.
✗ Incorrect
Using { timeout: 10000, ...options } sets timeout to 10000 by default but allows options.timeout to override it if provided. Other options either override timeout incorrectly or ignore options.