Test Overview
This test uses Cypress Testing Library to check if a button with the label 'Submit' is visible and clickable on the page. It verifies that the button exists and that clicking it triggers the expected behavior.
This test uses Cypress Testing Library to check if a button with the label 'Submit' is visible and clickable on the page. It verifies that the button exists and that clicking it triggers the expected behavior.
describe('Submit Button Test', () => { beforeEach(() => { cy.visit('/'); }); it('should find and click the Submit button', () => { cy.findByRole('button', { name: /submit/i }).should('be.visible').click(); cy.findByText(/form submitted/i).should('exist'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Cypress test runner initialized | — | PASS |
| 2 | Browser opens and navigates to '/' | Page loads with form containing a Submit button | Page loaded successfully | PASS |
| 3 | Find button with role 'button' and name matching 'Submit' using Cypress Testing Library | Submit button is present and visible on the page | Button is visible | PASS |
| 4 | Click the Submit button | Button is clicked, triggering form submission | — | PASS |
| 5 | Check for text 'Form Submitted' to confirm submission | Confirmation message displayed on page | Text 'Form Submitted' exists | PASS |
cypress-testing-library in your tests?findByRole with the role and accessible name.findByRole('button', { name: 'Submit' }), which is correct. cy.findByText('Submit') relies on text content without specifying the button role, making it less precise. cy.get('button').contains('Submit') uses Cypress core commands, not testing-library. cy.querySelector('button:contains("Submit")') is invalid syntax.cy.findByLabelText('Email').type('user@example.com')
cy.findByRole('button', { name: 'Send' }).click()findByLabelText finds an input by its label text, and type enters text. findByRole finds a button by role and name, then click clicks it.cy.findByRole('textbox', { name: 'Username' }).type('admin')
cy.findByRole('button', { name: 'Login' }).click()type works on textboxes. wait() is usually unnecessary.findAllByLabelText returns all matching elements. Using filter(':visible') narrows to visible ones, and first() picks the first visible input.Pass the test and this pattern moves unknown