cypress-testing-library helps you find and interact with web page elements like a user would. It makes tests easier to write and understand.
cypress-testing-library
import '@testing-library/cypress/add-commands'; // Example usage in a test cy.getByRole('button', { name: 'Submit' }).click();
Use getByRole to find elements by their role and accessible name, which matches how users see the page.
Import @testing-library/cypress/add-commands to extend Cypress with user-friendly selectors.
cy.getByText('Login').click();cy.getByRole('textbox', { name: 'Username' }).type('myuser');
cy.getByLabelText('Email').should('be.visible');
This test visits a login page, fills in username and password using labels, clicks the login button by its role and name, then checks if a welcome message appears.
import '@testing-library/cypress/add-commands'; describe('Login form test', () => { it('should allow user to login', () => { cy.visit('https://example.com/login'); cy.getByLabelText('Username').type('testuser'); cy.getByLabelText('Password').type('password123'); cy.getByRole('button', { name: 'Log In' }).click(); cy.getByText('Welcome, testuser!').should('be.visible'); }); });
Always import @testing-library/cypress/add-commands once to enable the commands.
Using roles and labels makes tests more accessible and closer to how real users find elements.
These selectors are less likely to break if the page layout changes but the user interface stays the same.
cypress-testing-library helps find page elements by text, role, or label like a user would.
It makes tests easier to read and maintain.
Use it to write tests that focus on user experience, not page structure.