0
0
Cypresstesting~5 mins

Cypress.Commands.add() - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is Cypress.Commands.add() used for?

Cypress.Commands.add() lets you create custom commands in Cypress. These commands help you reuse code and make tests cleaner and easier to read.

Click to reveal answer
beginner
How do you define a custom command named login using Cypress.Commands.add()?
Cypress.Commands.add('login', (email, password) => {<br>  cy.get('#email').type(email);<br>  cy.get('#password').type(password);<br>  cy.get('button[type=submit]').click();<br>});
Click to reveal answer
beginner
Why should you use custom commands in Cypress tests?

Custom commands help avoid repeating the same steps in many tests. They make tests shorter, easier to understand, and easier to maintain.

Click to reveal answer
intermediate
Can Cypress.Commands.add() accept multiple arguments? How?

Yes. You can pass multiple arguments to your custom command function. For example, Cypress.Commands.add('fillForm', (name, email) => { ... }).

Click to reveal answer
beginner
Where should you place your custom commands defined with Cypress.Commands.add()?

Place them in the cypress/support/commands.js file. This file loads automatically before your tests run.

Click to reveal answer
What does Cypress.Commands.add() do?
ADeletes a command
BRuns a test case
CCreates a new custom command
DStarts the Cypress server
Where is the best place to define custom commands in a Cypress project?
Acypress/support/commands.js
Bcypress.json
Ccypress/plugins/index.js
Dcypress/integration/tests.js
How do you call a custom command named login in a test?
Acy.login()
BCypress.login()
Clogin()
Dcy.Commands.login()
Which of these is a benefit of using Cypress.Commands.add()?
AMakes tests longer
BSlows down test execution
CRemoves test assertions
DAvoids code repetition
Can custom commands accept parameters?
ANo, they cannot
BYes, they can accept multiple parameters
CYes, but only one parameter
DOnly strings are allowed
Explain how to create and use a custom command with Cypress.Commands.add().
Think about how to reuse test steps easily.
You got /4 concepts.
    Why is it important to put custom commands in cypress/support/commands.js?
    Consider when Cypress loads support files.
    You got /3 concepts.