Bird
Raised Fist0
Cypresstesting~20 mins

Why custom commands reduce duplication in Cypress - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Command Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use custom commands in Cypress?

Which of the following best explains why custom commands help reduce duplication in Cypress tests?

AThey allow you to write reusable test steps that can be called multiple times, avoiding repeated code.
BThey automatically generate test data for each test case, reducing manual input.
CThey replace the need for assertions by validating all elements automatically.
DThey enable running tests in parallel without changing the test code.
Attempts:
2 left
💡 Hint

Think about how repeating the same steps in many tests can be simplified.

❓ Predict Output
intermediate
2:00remaining
Output of using a custom command

Given this Cypress custom command and test, what will be the output in the test runner console?

Cypress
Cypress.Commands.add('login', (user) => {
  cy.get('#username').type(user.name)
  cy.get('#password').type(user.password)
  cy.get('#login-button').click()
})

describe('Login Test', () => {
  it('logs in user', () => {
    cy.login({name: 'alice', password: '1234'})
    cy.url().should('include', '/dashboard')
  })
})
ATest passes but URL check is ignored due to missing assertion.
BTest fails because custom commands cannot accept arguments.
CTest fails because cy.get selectors are invalid without quotes.
DTest passes because the custom command logs in and URL includes '/dashboard'.
Attempts:
2 left
💡 Hint

Check if the custom command is used correctly and if the assertion matches the expected URL.

❓ assertion
advanced
2:00remaining
Correct assertion to verify custom command effect

Which assertion correctly verifies that a custom command successfully logged in a user by checking the presence of a logout button?

Cypress
Cypress.Commands.add('login', () => {
  cy.get('#username').type('user')
  cy.get('#password').type('pass')
  cy.get('#login-btn').click()
})

cy.login()
// Which assertion below is correct?
Acy.get('#login-btn').should('be.visible')
Bcy.get('#logout-btn').should('not.exist')
Ccy.get('#logout-btn').should('be.visible')
Dcy.get('#username').should('contain.text', 'user')
Attempts:
2 left
💡 Hint

After login, the logout button should appear on the page.

🔧 Debug
advanced
2:00remaining
Debugging duplicated code without custom commands

What is the main problem with this test code repeated in multiple tests?

Cypress
cy.get('#username').type('user')
cy.get('#password').type('pass')
cy.get('#login-btn').click()
cy.get('#logout-btn').should('be.visible')
ACustom commands are not allowed in Cypress tests.
BDuplicated code makes tests harder to maintain and update if login steps change.
CAssertions are missing to verify login success.
DThe selectors are invalid and cause syntax errors.
Attempts:
2 left
💡 Hint

Think about what happens if the login process changes and you have to update many tests.

❓ framework
expert
3:00remaining
Best practice for creating custom commands to reduce duplication

Which of the following is the best practice when creating custom commands in Cypress to reduce duplication and improve test clarity?

ACreate small, focused commands that perform one clear action and can be combined in tests.
BCreate one large command that performs all test steps to avoid writing multiple commands.
CAvoid using custom commands and write all steps directly in tests for clarity.
DUse custom commands only for assertions, not for actions like clicking or typing.
Attempts:
2 left
💡 Hint

Think about how modular code helps reuse and clarity.

Practice

(1/5)
1. Why do custom commands in Cypress help reduce duplication in test code?
easy
A. They replace the need for assertions in tests.
B. They automatically find bugs in your code.
C. They let you write common actions once and reuse them in many tests.
D. They make tests run faster by skipping steps.

Solution

  1. Step 1: Understand what duplication means in tests

    Duplication means writing the same code multiple times, which makes tests harder to maintain.
  2. Step 2: Role of custom commands in Cypress

    Custom commands let you write a common action once, like logging in, and reuse it everywhere, reducing repeated code.
  3. Final Answer:

    They let you write common actions once and reuse them in many tests. -> Option C
  4. Quick Check:

    Reuse common actions = less duplication [OK]
Hint: Custom commands = write once, use many times [OK]
Common Mistakes:
  • Thinking custom commands speed up test execution
  • Believing custom commands remove the need for assertions
  • Assuming custom commands automatically fix bugs
2. Which of the following is the correct way to define a custom command in Cypress?
easy
A. cy.addCommand('login', () => { cy.visit('/login') })
B. cy.Commands.create('login', () => { cy.visit('/login') })
C. Cypress.createCommand('login', () => { cy.visit('/login') })
D. Cypress.Commands.add('login', () => { cy.visit('/login') })

Solution

  1. Step 1: Recall Cypress syntax for custom commands

    The correct method to add a custom command is Cypress.Commands.add.
  2. Step 2: Check each option's syntax

    Only Cypress.Commands.add('login', () => { cy.visit('/login') }) uses Cypress.Commands.add correctly with the command name and function.
  3. Final Answer:

    Cypress.Commands.add('login', () => { cy.visit('/login') }) -> Option D
  4. Quick Check:

    Correct method is Cypress.Commands.add [OK]
Hint: Custom commands use Cypress.Commands.add() [OK]
Common Mistakes:
  • Using cy.Commands instead of Cypress.Commands
  • Using createCommand or addCommand which don't exist
  • Mixing up method names and object names
3. Given this custom command definition:
 Cypress.Commands.add('login', (user) => { cy.get('#username').type(user.name); cy.get('#password').type(user.pass); cy.get('#submit').click(); }); 

What will happen when you run this test code?
 cy.login({name: 'alice', pass: '1234'}); cy.url().should('include', '/dashboard'); 
medium
A. The test types username and password, clicks submit, then checks URL includes '/dashboard'.
B. The test will fail because custom commands cannot take arguments.
C. The test will skip typing password and only type username.
D. The test will throw a syntax error due to missing return statement.

Solution

  1. Step 1: Understand the custom command behavior

    The 'login' command types the username and password from the passed object and clicks submit.
  2. Step 2: Analyze the test code execution

    Calling cy.login with user data runs the command, then the test asserts the URL includes '/dashboard'.
  3. Final Answer:

    The test types username and password, clicks submit, then checks URL includes '/dashboard'. -> Option A
  4. Quick Check:

    Custom command with args runs actions then assertion passes [OK]
Hint: Custom commands can take arguments and run steps [OK]
Common Mistakes:
  • Thinking custom commands can't accept parameters
  • Assuming missing return causes syntax error
  • Believing some steps are skipped automatically
4. You wrote this custom command:
 Cypress.Commands.add('clickButton', () => { cy.get('button.submit').click(); }); 

But your tests fail with "Cannot read property 'click' of undefined". What is the likely problem?
medium
A. You forgot to call cy.visit() before using the command.
B. The selector 'button.submit' does not match any element on the page.
C. Custom commands cannot use cy.get inside them.
D. The command name 'clickButton' is reserved and cannot be used.

Solution

  1. Step 1: Understand the error message

    "Cannot read property 'click' of undefined" means cy.get did not find the element, so it returned undefined.
  2. Step 2: Check possible causes

    If the selector does not match any element, cy.get returns nothing, causing the error when calling click.
  3. Final Answer:

    The selector 'button.submit' does not match any element on the page. -> Option B
  4. Quick Check:

    Selector missing element = click fails [OK]
Hint: Check if selector matches elements before clicking [OK]
Common Mistakes:
  • Assuming cy.get can't be used in custom commands
  • Forgetting to visit the page (may cause no elements)
  • Thinking command names are reserved keywords
5. You want to create a custom command fillForm that fills multiple fields on a page. Which approach best reduces duplication and improves test readability?
hard
A. Create fillForm to accept an object with field names and values, then fill each field inside the command.
B. Write separate commands for each field and call them one by one in tests.
C. Copy and paste the same cy.get().type() lines in every test needing the form filled.
D. Use cy.exec() to run external scripts that fill the form.

Solution

  1. Step 1: Consider duplication and readability

    Copy-pasting code (Copy and paste the same cy.get().type() lines in every test needing the form filled.) causes duplication and harder maintenance.
  2. Step 2: Evaluate options for custom commands

    Creating one command that accepts an object and fills all fields (Create fillForm to accept an object with field names and values, then fill each field inside the command.) centralizes logic and improves readability.
  3. Step 3: Analyze other options

    Separate commands (Write separate commands for each field and call them one by one in tests.) increase calls and complexity; cy.exec (Use cy.exec() to run external scripts that fill the form.) is not for UI actions.
  4. Final Answer:

    Create fillForm to accept an object with field names and values, then fill each field inside the command. -> Option A
  5. Quick Check:

    One command with params reduces duplication best [OK]
Hint: Use one paramized command for multiple fields [OK]
Common Mistakes:
  • Writing many small commands instead of one flexible one
  • Copy-pasting code in tests instead of reusing commands
  • Using cy.exec for UI interactions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard