Clear text from an input field using cy.clear()
Preconditions (2)
✅ Expected Result: The input field with id 'username' is empty after calling cy.clear()
describe('Input field clear test', () => { it('should clear the input field using cy.clear()', () => { cy.visit('https://example.com'); cy.get('#username') .type('testuser') .should('have.value', 'testuser') .clear() .should('have.value', ''); }); });
This test visits the target page, selects the input field by its id username, types the text testuser, and asserts the input contains that text. Then it calls cy.clear() to clear the input field and asserts that the input is empty by checking its value is an empty string. Using chaining ensures commands run in order and Cypress waits automatically for each step to complete.
Now add data-driven testing with 3 different input values to verify clearing works for each
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