0
0
Cypresstesting~20 mins

Why custom commands reduce duplication in Cypress - Challenge Your Understanding

Choose your learning style9 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.