Which of the following best explains why custom commands help reduce duplication in Cypress tests?
Think about how repeating the same steps in many tests can be simplified.
Custom commands let you write a piece of code once and reuse it in many tests. This avoids copying the same code multiple times, making tests easier to maintain.
Given this Cypress custom command and test, what will be the output in the test runner console?
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') }) })
Check if the custom command is used correctly and if the assertion matches the expected URL.
The custom command 'login' types username and password, then clicks login. The test then checks if the URL contains '/dashboard'. This is a valid test and should pass if the app behaves correctly.
Which assertion correctly verifies that a custom command successfully logged in a user by checking the presence of a logout button?
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?
After login, the logout button should appear on the page.
After a successful login, the logout button should be visible. Checking for its visibility confirms the login worked.
What is the main problem with this test code repeated in multiple tests?
cy.get('#username').type('user') cy.get('#password').type('pass') cy.get('#login-btn').click() cy.get('#logout-btn').should('be.visible')
Think about what happens if the login process changes and you have to update many tests.
Repeating the same login steps in many tests means if the login changes, you must update all tests. This wastes time and risks errors. Custom commands solve this by centralizing the code.
Which of the following is the best practice when creating custom commands in Cypress to reduce duplication and improve test clarity?
Think about how modular code helps reuse and clarity.
Small, focused custom commands are easier to reuse and combine. They keep tests clear and maintainable. Large commands can hide details and reduce flexibility.