It helps you write tests that find elements the way users do, by their text or role, making tests easier to read and maintain.
Click to reveal answer
beginner
How does cypress-testing-library improve test selectors compared to CSS selectors?
It encourages using queries like findByRole or findByText that match what users see, instead of fragile CSS selectors that can break easily.
Click to reveal answer
beginner
What command do you use to install cypress-testing-library?
Run npm install --save-dev @testing-library/cypress to add it to your project.
Click to reveal answer
intermediate
How do you add cypress-testing-library commands to Cypress?
Import <code>@testing-library/cypress/add-commands</code> in your <code>cypress/support/e2e.js</code> file to enable its commands.
Click to reveal answer
intermediate
Give an example of using cypress-testing-library to find a button by its role and click it.
Example: cy.findByRole('button', { name: /submit/i }).click() This finds a button with accessible name 'submit' and clicks it.
Click to reveal answer
Which command is provided by cypress-testing-library to find elements by their accessible role?
AquerySelector
BgetByClass
CfindByRole
DgetById
✗ Incorrect
The findByRole command finds elements by their ARIA role, matching how users identify elements.
What is the main benefit of using cypress-testing-library selectors over CSS selectors?
AThey match user-visible text or roles, making tests more reliable
BThey are faster to run
CThey require less code
DThey only work with buttons
✗ Incorrect
Selectors based on user-visible text or roles are more stable and reflect how users interact with the app.
Where should you import @testing-library/cypress/add-commands in your Cypress project?
Acypress/plugins/index.js
Bcypress/support/e2e.js
Ccypress.json
Dpackage.json
✗ Incorrect
The support file e2e.js is the right place to add custom commands for Cypress.
Which of these is a valid way to find a button with the text 'Submit' using cypress-testing-library?
Acy.findByRole('button', { name: /submit/i })
Bcy.findById('submit')
Ccy.get('button.submit')
Dcy.findByText('Submit')
✗ Incorrect
Using findByRole with the accessible name option is the recommended way to find buttons.
What does the cy.findByText() command do?
AFinds elements by their CSS class
BFinds elements by their HTML tag
CFinds elements by their ID
DFinds elements containing specific visible text
✗ Incorrect
findByText searches for elements that contain the given visible text, similar to how a user reads the page.
Explain how cypress-testing-library helps improve the reliability and readability of Cypress tests.
Think about how users find elements on a page.
You got /4 concepts.
Describe the steps to set up cypress-testing-library in a new Cypress project.
Focus on installation and configuration.
You got /3 concepts.
Practice
(1/5)
1. What is the main benefit of using cypress-testing-library in your tests?
easy
A. It helps find elements like a user would, improving test readability.
B. It automatically fixes broken tests.
C. It replaces Cypress commands with JavaScript alerts.
D. It only works with CSS selectors.
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 A
Quick Check:
User-focused element finding = A [OK]
Hint: Remember: testing-library = user-like element finding [OK]
Common Mistakes:
Thinking it fixes tests automatically
Confusing it with Cypress core commands
Assuming it only uses CSS selectors
2. Which of the following is the correct syntax to find a button with the text 'Submit' using cypress-testing-library?
easy
A. cy.findByRole('button', { name: 'Submit' })
B. cy.findByText('Submit')
C. cy.get('button').contains('Submit')
D. cy.querySelector('button:contains("Submit")')
Solution
Step 1: Recall cypress-testing-library syntax for roles
The recommended way is to use findByRole with the role and accessible name.
Step 2: Check each option
cy.findByRole('button', { name: 'Submit' }) uses 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.
Final Answer:
cy.findByRole('button', { name: 'Submit' }) -> Option A
Quick Check:
Use findByRole with name for buttons = B [OK]
Hint: Use findByRole with name for accessible buttons [OK]
Common Mistakes:
Using Cypress core commands instead of testing-library
But the test fails with "Unable to find element" error on the textbox. What is the likely cause?
medium
A. The test is missing a wait() before typing.
B. The textbox does not have an accessible name 'Username'.
C. The type command is not supported on textboxes.
D. The button role is incorrect.
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, and type works on textboxes. wait() is usually unnecessary.
Final Answer:
The textbox does not have an accessible name 'Username'. -> Option B
Quick Check:
Accessible name missing causes find failure = D [OK]
Hint: Check accessible names if findByRole fails [OK]
Common Mistakes:
Assuming role or command is wrong
Adding unnecessary waits
Ignoring accessibility attributes
5. You want to test a form with multiple inputs labeled 'Name' but only one is visible at a time. Which cypress-testing-library method helps you select the visible input labeled 'Name' reliably?
hard
A. cy.findByLabelText('Name', { exact: false })
B. cy.findByLabelText('Name', { selector: ':visible' })
C. cy.findAllByLabelText('Name').filter(':visible').first()
D. cy.findByText('Name').filter(':visible')
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
findAllByLabelText returns all matching elements. Using filter(':visible') narrows to visible ones, and first() picks the first visible input.
Final Answer:
cy.findAllByLabelText('Name').filter(':visible').first() -> Option C
Quick Check:
Use findAll + filter visible + first = C [OK]
Hint: Use findAllByLabelText + filter(':visible') + first() [OK]
Common Mistakes:
Using findByLabelText with invalid selector option
Using findByText instead of label text
Ignoring visibility filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.