0
0
Cypresstesting~20 mins

Cypress.Commands.add() - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cypress Commands Master
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 custom command usage?
Consider this Cypress custom command added to support login:
 Cypress.Commands.add('login', (username, password) => {
cy.get('#user').type(username);
cy.get('#pass').type(password);
cy.get('#submit').click();
});

What will happen when the test runs this code?
 cy.login('user1', 'pass123'); 
Cypress
Cypress.Commands.add('login', (username, password) => {
  cy.get('#user').type(username);
  cy.get('#pass').type(password);
  cy.get('#submit').click();
});

cy.login('user1', 'pass123');
AThe test only clicks #submit without typing anything.
BThe test throws an error because cy.login is not a valid Cypress command.
CThe test types 'login' in #user and 'user1' in #pass, then clicks #submit.
DThe test types 'user1' in #user, 'pass123' in #pass, then clicks #submit.
Attempts:
2 left
💡 Hint
Remember that Cypress.Commands.add creates a new command callable by cy..
assertion
intermediate
2:00remaining
Which assertion correctly verifies the custom command was called?
You added a custom command 'fillForm' that types into inputs and clicks submit.
How do you assert that the command was called once during the test?
Cypress
Cypress.Commands.add('fillForm', () => {
  cy.get('#name').type('Alice');
  cy.get('#email').type('alice@example.com');
  cy.get('#submit').click();
});

// Test code
cy.fillForm();
Acy.spy(Cypress.Commands, 'fillForm').should('have.been.calledOnce');
Bcy.stub(Cypress.Commands, 'fillForm').should('have.been.calledOnce');
Ccy.get('@fillForm').should('have.been.calledOnce');
Dcy.get('fillForm').should('have.been.calledOnce');
Attempts:
2 left
💡 Hint
Use cy.spy to watch if a function was called.
🔧 Debug
advanced
2:00remaining
Why does this custom command fail to run?
This custom command is added:
Cypress.Commands.add('clickButton', () => {
  cy.get('.btn').click
});

When the test calls cy.clickButton(), it fails. Why?
Cypress
Cypress.Commands.add('clickButton', () => {
  cy.get('.btn').click
});

cy.clickButton();
ABecause custom commands cannot use arrow functions.
BBecause cy.get('.btn').click is missing parentheses to invoke the click function.
CBecause '.btn' selector is invalid and causes an error.
DBecause cy.clickButton() is not a valid Cypress command.
Attempts:
2 left
💡 Hint
Check if all functions are properly called.
🧠 Conceptual
advanced
1:30remaining
What is the main benefit of using Cypress.Commands.add()?
Why do testers create custom commands with Cypress.Commands.add() instead of writing the same steps repeatedly?
ATo reuse common test steps easily and keep tests clean and readable.
BTo make tests run faster by compiling commands into native code.
CTo avoid writing any selectors in tests.
DTo automatically generate test reports.
Attempts:
2 left
💡 Hint
Think about code reuse and maintenance.
framework
expert
2:30remaining
How to properly add a custom command that returns a value for chaining?
You want to add a custom command 'getUserName' that gets text from '#username' and returns it for chaining.
Which code correctly implements this?
Cypress
Cypress.Commands.add('getUserName', () => {
  return cy.get('#username').invoke('text');
});
ACypress.Commands.add('getUserName', () => { cy.get('#username').invoke('text'); });
BCypress.Commands.add('getUserName', () => { return cy.get('#username').text(); });
CCypress.Commands.add('getUserName', () => cy.get('#username').invoke('text'));
DCypress.Commands.add('getUserName', () => cy.get('#username').text());
Attempts:
2 left
💡 Hint
Remember to return the Cypress chainable for chaining.