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.
Practice
cypress-testing-library in your tests?Solution
Step 1: Understand the purpose of cypress-testing-library
It is designed to find page elements by text, role, or label, mimicking how users interact with the page.Step 2: Compare options with this purpose
Only It helps find elements like a user would, improving test readability. correctly states that it helps find elements like a user would and improves test readability.Final Answer:
It helps find elements like a user would, improving test readability. -> Option AQuick Check:
User-focused element finding = A [OK]
- Thinking it fixes tests automatically
- Confusing it with Cypress core commands
- Assuming it only uses CSS selectors
Solution
Step 1: Recall cypress-testing-library syntax for roles
The recommended way is to usefindByRolewith the role and accessible name.Step 2: Check each option
cy.findByRole('button', { name: 'Submit' }) usesfindByRole('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.Final Answer:
cy.findByRole('button', { name: 'Submit' }) -> Option AQuick Check:
Use findByRole with name for buttons = B [OK]
- Using Cypress core commands instead of testing-library
- Missing the role parameter
- Using invalid selectors like querySelector
cy.findByLabelText('Email').type('user@example.com')
cy.findByRole('button', { name: 'Send' }).click()What will happen when this test runs?
Solution
Step 1: Understand the commands used
findByLabelTextfinds an input by its label text, andtypeenters text.findByRolefinds a button by role and name, thenclickclicks it.Step 2: Analyze the expected behavior
Both commands are valid and should work if elements exist. So the test will type the email and click the button.Final Answer:
It types the email and clicks the 'Send' button successfully. -> Option DQuick Check:
Valid findByLabelText and findByRole = A [OK]
- Assuming findByLabelText is invalid
- Confusing Cypress core commands with testing-library
- Thinking click happens before typing
cy.findByRole('textbox', { name: 'Username' }).type('admin')
cy.findByRole('button', { name: 'Login' }).click()But the test fails with "Unable to find element" error on the textbox. What is the likely cause?
Solution
Step 1: Understand the error message
"Unable to find element" means the locator did not find the textbox with role 'textbox' and name 'Username'.Step 2: Check possible causes
If the textbox lacks an accessible name 'Username', the locator fails. The button role is likely correct, andtypeworks on textboxes.wait()is usually unnecessary.Final Answer:
The textbox does not have an accessible name 'Username'. -> Option BQuick Check:
Accessible name missing causes find failure = D [OK]
- Assuming role or command is wrong
- Adding unnecessary waits
- Ignoring accessibility attributes
Solution
Step 1: Identify the problem
Multiple inputs share the same label 'Name', but only one is visible at a time.Step 2: Choose method to select visible input
findAllByLabelTextreturns all matching elements. Usingfilter(':visible')narrows to visible ones, andfirst()picks the first visible input.Final Answer:
cy.findAllByLabelText('Name').filter(':visible').first() -> Option CQuick Check:
Use findAll + filter visible + first = C [OK]
- Using findByLabelText with invalid selector option
- Using findByText instead of label text
- Ignoring visibility filtering
Ready to prove it?
Pass the test and this pattern moves unknown
