Cypress.Commands.add() lets you create your own custom commands to reuse steps easily in tests.
Cypress.Commands.add()
Introduction
Syntax
Cypress
Cypress.Commands.add('commandName', (arg1, arg2, ...) => { // your custom command code here });
The first argument is the name of your new command as a string.
The second argument is a function that runs when you call the command.
Examples
Cypress
Cypress.Commands.add('login', (email, password) => { cy.get('#email').type(email); cy.get('#password').type(password); cy.get('button[type=submit]').click(); });
Cypress
Cypress.Commands.add('clickButton', (label) => { cy.contains('button', label).click(); });
Sample Program
This test uses the custom 'login' command to enter credentials and check if the user reaches the dashboard page.
Cypress
Cypress.Commands.add('login', (email, password) => { cy.get('#email').type(email); cy.get('#password').type(password); cy.get('button[type=submit]').click(); }); describe('Login Test', () => { it('logs in with valid credentials', () => { cy.visit('https://example.com/login'); cy.login('user@example.com', 'mypassword'); cy.url().should('include', '/dashboard'); }); });
Important Notes
Custom commands help keep tests DRY (Don't Repeat Yourself).
Always choose clear command names to make tests easy to read.
You can add commands for any repeated action in your tests.
Summary
Cypress.Commands.add() creates reusable custom commands.
Use it to simplify and clean your test code.
Call your custom commands like built-in Cypress commands.
Practice
1. What is the main purpose of
Cypress.Commands.add() in Cypress testing?easy
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]
Hint: Remember: add() creates custom commands you can reuse [OK]
Common Mistakes:
- Thinking it adds test cases automatically
- Confusing it with test report generation
- Assuming it changes browser settings
2. Which of the following is the correct syntax to add a custom command named
login in Cypress?easy
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]
Hint: Use Cypress.Commands.add('name', function) always [OK]
Common Mistakes:
- Using Cypress.addCommand instead of Commands.add
- Omitting Cypress.Commands prefix
- Using create instead of add
3. Given the code below, what will be the output when
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')medium
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]
Hint: Match parameters to fields carefully [OK]
Common Mistakes:
- Assuming no parameters are needed
- Mixing up username and password fields
- Ignoring the click action
4. Identify the error in the following custom command definition:
Cypress.Commands.add('clickButton', () => {
cy.get('#button')
cy.click()
})medium
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]
Hint: Always chain commands like cy.get(...).click() [OK]
Common Mistakes:
- Calling cy.click() separately
- Changing command name unnecessarily
- Using cy.find() instead of cy.get() without reason
5. You want to create a custom command
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?hard
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]
Hint: Pass one object with named fields for clarity [OK]
Common Mistakes:
- Using undefined variables without parameters
- Passing multiple parameters instead of one object
- Accessing object fields by index instead of name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
