Challenge - 5 Problems
Cypress Commands Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this custom command usage?
Consider this Cypress custom command added to support login:
What will happen when the test runs this code?
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');
Attempts:
2 left
💡 Hint
Remember that Cypress.Commands.add creates a new command callable by cy..
✗ Incorrect
The custom command 'login' is defined to type the username and password into the respective fields and then click submit. Calling cy.login('user1', 'pass123') runs this sequence exactly.
❓ assertion
intermediate2: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?
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();
Attempts:
2 left
💡 Hint
Use cy.spy to watch if a function was called.
✗ Incorrect
Using cy.spy on Cypress.Commands for 'fillForm' lets you check if the command was called. The other options misuse stubs or selectors.
🔧 Debug
advanced2:00remaining
Why does this custom command fail to run?
This custom command is added:
When the test calls cy.clickButton(), it fails. Why?
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();
Attempts:
2 left
💡 Hint
Check if all functions are properly called.
✗ Incorrect
The code references cy.get('.btn').click without parentheses, so the click function is not executed. Adding () fixes it.
🧠 Conceptual
advanced1: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?
Attempts:
2 left
💡 Hint
Think about code reuse and maintenance.
✗ Incorrect
Custom commands help reuse common sequences, making tests simpler and easier to maintain. They do not affect speed or reporting directly.
❓ framework
expert2: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?
Which code correctly implements this?
Cypress
Cypress.Commands.add('getUserName', () => { return cy.get('#username').invoke('text'); });
Attempts:
2 left
💡 Hint
Remember to return the Cypress chainable for chaining.
✗ Incorrect
Option C returns the chainable correctly. Option C misses return, so returns undefined. Options C and D use .text() which is not a Cypress command and causes errors.