Verify login form elements using cy.get() with CSS selectors
Preconditions (1)
✅ Expected Result: The welcome message element is visible and contains the text 'Welcome, user!'
describe('Login form test using cy.get() with CSS selectors', () => { beforeEach(() => { cy.visit('https://example.com/login'); }); it('should login successfully and show welcome message', () => { cy.get('#email').should('be.visible').type('user@example.com'); cy.get('.password-input').should('be.visible').type('Password123'); cy.get('button[type="submit"]').should('be.enabled').click(); cy.get('.welcome-message').should('be.visible').and('have.text', 'Welcome, user!'); }); });
This test script uses Cypress to automate the login form test.
First, cy.visit() opens the login page.
Then, cy.get() with CSS selectors finds each element: the email input by #email, the password input by .password-input, and the login button by button[type="submit"].
Before typing or clicking, .should() asserts the element is visible or enabled to avoid errors.
After clicking login, it checks the welcome message is visible and has the exact text 'Welcome, user!'.
This approach ensures the test waits for elements properly and uses clear, maintainable selectors.
Now add data-driven testing with 3 different sets of email and password inputs
cy.get() do in Cypress testing?cy.get()cy.get() is used to locate elements on the web page by using CSS selectors.cy.get() only finds elements.btn-primary using cy.get()?.btn-primary selects elements with that class.<ul> <li class='item'>Apple</li> <li class='item'>Banana</li> <li class='item'>Cherry</li> </ul>
cy.get('.item').should('have.length', 3) check?cy.get('.item').should('have.length', 3)cy.get('button#submit').click() but the test fails with "Cannot find element" error. What is the most likely cause?button#submitcy.get('#submit button') reverses the selector incorrectly. You must use cy.find() instead of cy.get() is wrong because cy.get() is correct for global selection. The click() command is not supported on buttons is false; click() works on buttons.<li> element inside a <ul id='menu'>. Which cy.get() selector is correct?li:nth-child(2) selects the second child <li> element inside its parent.Pass the test and this pattern moves unknown