What if you could write a test step once and use it everywhere without copying?
Why Cypress.Commands.add()? - Purpose & Use Cases
Imagine testing a website where you have to log in many times manually by typing the username and password each time in your test scripts.
You copy and paste the same steps over and over in every test.
This manual way is slow and boring.
It's easy to make mistakes copying the same steps repeatedly.
If the login process changes, you must update every test, which wastes time and causes errors.
Cypress.Commands.add() lets you create your own custom commands.
You write the login steps once, then reuse them everywhere by calling your new command.
This saves time, reduces mistakes, and makes tests easier to update.
cy.get('#username').type('user') cy.get('#password').type('pass') cy.get('#login').click()
Cypress.Commands.add('login', () => { cy.get('#username').type('user') cy.get('#password').type('pass') cy.get('#login').click() }) cy.login()
You can build a library of reusable commands that make your tests cleaner, faster, and easier to maintain.
In a big project, you create a custom command for logging in once.
Every test just calls cy.login() instead of repeating the login steps.
When the login changes, you update only one place.
Manual repetition of test steps is slow and error-prone.
Cypress.Commands.add() creates reusable custom commands.
Custom commands make tests simpler and easier to maintain.
Practice
Cypress.Commands.add() in Cypress testing?Solution
Step 1: Understand the function of Cypress.Commands.add()
This method allows you to define your own commands that you can reuse in tests.Step 2: Identify the benefit of reusable commands
Reusable commands help keep test code clean and avoid repetition.Final Answer:
To create reusable custom commands for cleaner test code -> Option CQuick Check:
Reusable commands = Cleaner code [OK]
- Thinking it adds test cases automatically
- Confusing it with test report generation
- Assuming it changes browser settings
login in Cypress?Solution
Step 1: Recall the correct method name
The correct method to add commands isCypress.Commands.add().Step 2: Check the syntax format
The first argument is the command name as a string, the second is a function.Final Answer:
Cypress.Commands.add('login', () => { /* code */ }) -> Option DQuick Check:
Correct method and syntax = Cypress.Commands.add('login', () => { /* code */ }) [OK]
- Using Cypress.addCommand instead of Commands.add
- Omitting Cypress.Commands prefix
- Using create instead of add
cy.login() is called in a test? Cypress.Commands.add('login', (username, password) => {
cy.get('#user').type(username)
cy.get('#pass').type(password)
cy.get('#submit').click()
})
// In test
cy.login('user1', 'pass1')Solution
Step 1: Analyze the custom command parameters
The command takes username and password and types them into respective fields.Step 2: Follow the command actions
It types username into '#user', password into '#pass', then clicks '#submit'.Final Answer:
Types 'user1' in #user, 'pass1' in #pass, then clicks #submit -> Option AQuick Check:
Parameters map to correct fields = Types 'user1' in #user, 'pass1' in #pass, then clicks #submit [OK]
- Assuming no parameters are needed
- Mixing up username and password fields
- Ignoring the click action
Cypress.Commands.add('clickButton', () => {
cy.get('#button')
cy.click()
})Solution
Step 1: Review the command chaining
cy.get('#button')returns an element, butcy.click()is called separately, not chained.Step 2: Understand correct chaining
Click should be chained to the element:cy.get('#button').click().Final Answer:
Missing chaining ofclick()tocy.get()-> Option AQuick Check:
Method chaining required = Missing chaining ofclick()tocy.get()[OK]
- Calling cy.click() separately
- Changing command name unnecessarily
- Using cy.find() instead of cy.get() without reason
fillForm that fills multiple fields and submits a form. Which approach correctly uses Cypress.Commands.add() to accept an object with field values and perform the actions?Solution
Step 1: Understand parameter passing
Using a single object parameterdatawith named properties is clear and flexible.Step 2: Check usage of object properties
Accessingdata.nameanddata.emailmatches the object keys.Final Answer:
Cypress.Commands.add('fillForm', (data) => { cy.get('#name').type(data.name) cy.get('#email').type(data.email) cy.get('#submit').click() }) -> Option BQuick Check:
Object parameter with named fields = Cypress.Commands.add('fillForm', (data) => { cy.get('#name').type(data.name) cy.get('#email').type(data.email) cy.get('#submit').click() }) [OK]
- Using undefined variables without parameters
- Passing multiple parameters instead of one object
- Accessing object fields by index instead of name
Ready to prove it?
Pass the test and this pattern moves unknown
