0
0
Cypresstesting~20 mins

Overwriting existing commands in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 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.