Bird
Raised Fist0
Cypresstesting~20 mins

Overwriting existing commands in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cypress Command Overwriter
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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();
  });
});
AThe test logs 'Custom click called' once and clicks the first link successfully.
BThe test fails because the overwrite does not call the original function.
CThe test logs 'Custom click called' multiple times but does not perform the click.
DThe test throws a syntax error due to incorrect overwrite syntax.
Attempts:
2 left
💡 Hint
Remember that the overwrite calls the original function after logging.
❓ assertion
intermediate
2: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');
Acy.get('input').type('hello').should('have.value', 'Custom type called');
Bcy.get('input').should('contain', 'Custom type called');
Ccy.get('input').type('hello').should('have.text', 'Custom type called');
Dcy.spy(cy, 'log').should('have.been.calledWith', 'Custom type called');
Attempts:
2 left
💡 Hint
Think about how to check if a Cypress command like cy.log was called with a specific argument.
🔧 Debug
advanced
2: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);
});
ABecause the overwrite calls cy.get('button').click() recursively causing infinite loop.
BBecause cy.log is asynchronous and blocks the command chain.
CBecause originalFn is called without subject causing an error.
DBecause the overwrite does not return anything.
Attempts:
2 left
💡 Hint
Think about what happens when the overwritten command calls itself inside.
🧠 Conceptual
advanced
2: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?
ATo prevent Cypress from logging commands in the test runner.
BTo change the default behavior of existing commands globally without changing test code.
CTo make tests run faster by skipping command execution.
DTo disable commands temporarily during test execution.
Attempts:
2 left
💡 Hint
Think about how overwriting affects all uses of a command.
❓ framework
expert
3: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
});
Areturn originalFn(url, options || { timeout: 10000 });
Breturn originalFn(url, { ...options, timeout: 10000 });
Creturn originalFn(url, { timeout: 10000, ...options });
Dreturn originalFn(url, options.timeout ? options : { timeout: 10000 });
Attempts:
2 left
💡 Hint
Remember that later properties in object spread override earlier ones.

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