Test Overview
This test opens a web page with a text input field, types some text into it, then clears the input using cy.clear(). It verifies that the input field is empty after clearing.
This test opens a web page with a text input field, types some text into it, then clears the input using cy.clear(). It verifies that the input field is empty after clearing.
describe('Input field clear test', () => { it('should clear the input field after typing', () => { cy.visit('https://example.cypress.io/commands/actions') cy.get('.action-email') .type('hello@example.com') .should('have.value', 'hello@example.com') .clear() .should('have.value', '') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, no browser opened yet | — | PASS |
| 2 | Browser opens | Browser window opens, ready to load page | — | PASS |
| 3 | Navigates to 'https://example.cypress.io/commands/actions' | Page loads with various input fields visible | Page URL is correct | PASS |
| 4 | Finds input element with class '.action-email' | Input field for email is visible and enabled | Element found and visible | PASS |
| 5 | Types 'hello@example.com' into the input field | Input field value is now 'hello@example.com' | Input value equals 'hello@example.com' | PASS |
| 6 | Clears the input field using cy.clear() | Input field is empty | Input value equals '' (empty string) | PASS |
| 7 | Test ends | Browser remains open or closes depending on config | — | PASS |
cy.clear() command do in Cypress when used on an input field?cy.clear()cy.clear() command is designed to remove any existing text from an input field.username using Cypress?cy.get(), then call .clear() on the element.cy.get('#username').clear(); is correct.cy.get('#email').type('hello@example.com');
cy.get('#email').clear();
cy.get('#email').type('user@domain.com');clear() empties the input. Finally, 'user@domain.com' is typed.clear() removes previous text, the final input contains only 'user@domain.com'.cy.get('#search').clear;clear without parentheses, so the function is not executed.clear() with parentheses to invoke the method.clear is missing parentheses to call the function. -> Option C.user-input might already contain text. Which Cypress code snippet correctly clears the input and types 'new text' ensuring no leftover text remains?cy.clear() is the Cypress command designed to empty input fields before typing..clear().type('new text') ensures the field is emptied before new text is entered.clearText() does not exist; invoke('attr', 'value', '') sets the HTML attribute but does not clear the current input value.Pass the test and this pattern moves unknown