cypress-testing-library - Build an Automation Script
import '@testing-library/cypress/add-commands'; describe('Search Page Tests', () => { beforeEach(() => { cy.visit('/search'); }); it('allows user to search and see results', () => { // Find the search input by its label and type 'apple' cy.findByLabelText('Search').type('apple'); // Find the submit button by role and name and click it cy.findByRole('button', { name: 'Submit Search' }).click(); // Verify that at least one search result containing 'apple' is visible cy.findAllByText(/apple/i).should('be.visible').and('have.length.greaterThan', 0); }); });
The test imports the testing-library commands to use accessibility-friendly selectors.
Before each test, it visits the '/search' page as per precondition.
It finds the search input by its label 'Search' and types 'apple'.
Then it finds the submit button by its role 'button' and accessible name 'Submit Search' and clicks it.
Finally, it asserts that at least one visible element contains the text 'apple' (case-insensitive), ensuring search results appear.
This approach uses best practices by relying on accessible selectors and Cypress's automatic waiting for elements.
Now add data-driven testing with 3 different search terms: 'apple', 'banana', 'cherry'
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
