Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a custom command in Cypress.
Cypress
Cypress.Commands.[1]('login', (email, password) => { cy.get('#email').type(email); cy.get('#password').type(password); cy.get('button[type=submit]').click(); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'create' or 'register' instead of 'add' causes errors.
Misspelling the method name.
✗ Incorrect
The correct method to add a custom command in Cypress is 'add'.
2fill in blank
mediumComplete the code to use the custom 'login' command in a test.
Cypress
describe('User Login', () => { it('logs in successfully', () => { cy.[1]('user@example.com', 'password123'); cy.url().should('include', '/dashboard'); }); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different command name than defined.
Forgetting to call the command on 'cy'.
✗ Incorrect
The custom command defined was named 'login', so we use cy.login().
3fill in blank
hardFix the error in the custom command definition to avoid duplication.
Cypress
Cypress.Commands.add('login', (email, password) => { cy.get('#email').type(email); cy.get('#password').[1](password); cy.get('button[type=submit]').click(); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'input' or 'fill'.
Misspelling 'type'.
✗ Incorrect
The correct Cypress command to enter text is 'type'.
4fill in blank
hardFill both blanks to create a custom command that clears and types text.
Cypress
Cypress.Commands.add('clearAndType', (selector, text) => { cy.get(selector).[1](); cy.get(selector).[2](text); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'click' or 'focus' instead of 'clear'.
Typing before clearing the input.
✗ Incorrect
First clear the input with 'clear()', then type text with 'type()'.
5fill in blank
hardFill all three blanks to create a custom command that logs in and verifies URL.
Cypress
Cypress.Commands.add('loginAndCheck', (email, password, urlPart) => { cy.get('#email').[1](email); cy.get('#password').[2](password); cy.get('button[type=submit]').click(); cy.url().should([3], urlPart); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'click' instead of 'type' for inputs.
Using 'contain' which is not a valid assertion method in Cypress.
Forgetting to check URL after login.
✗ Incorrect
Use 'type' to enter email and password, then check URL includes the expected part with 'include'.