Challenge - 5 Problems
Cypress Testing Library Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the test result of this Cypress code snippet?
Consider this Cypress test using cypress-testing-library commands. What will be the test execution result?
Cypress
describe('Button click test', () => { it('should find and click the submit button', () => { cy.visit('/form') cy.findByRole('button', { name: /submit/i }).click() cy.findByText(/form submitted/i).should('exist') }) })
Attempts:
2 left
💡 Hint
Remember that cypress-testing-library extends Cypress with findByRole and findByText commands.
✗ Incorrect
The test uses valid cypress-testing-library commands to find a button by its role and name, clicks it, then asserts that confirmation text appears. This is a correct and common usage pattern.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies an input field is disabled using cypress-testing-library?
You want to check if an input field with label 'Email' is disabled. Which assertion is correct?
Attempts:
2 left
💡 Hint
Use the correct Cypress assertion for disabled elements.
✗ Incorrect
The assertion 'should('be.disabled')' correctly checks if the element is disabled. Option A is incorrect because the disabled attribute is either present or not; false is not a valid value. Option A is logically correct but less clear than B. Option A checks for enabled, which is opposite.
❓ locator
advanced1:30remaining
Which locator best follows testing-library best practices to find a checkbox labeled 'Accept Terms'?
Choose the best locator to find the checkbox input associated with the label 'Accept Terms' using cypress-testing-library.
Attempts:
2 left
💡 Hint
Testing-library encourages queries by role with accessible name.
✗ Incorrect
Using findByRole with role 'checkbox' and name 'Accept Terms' is the best practice as it mimics how users perceive the UI. Option D works but is less explicit about role. Options B and D rely on implementation details, which is discouraged.
🔧 Debug
advanced2:00remaining
Why does this cypress-testing-library test fail to find the button?
Given the code below, why does the test fail to find the button labeled 'Save'?
cy.visit('/settings')
cy.findByRole('button', { name: 'Save' }).click()
Attempts:
2 left
💡 Hint
Consider DOM context and accessibility tree when using findByRole.
✗ Incorrect
cypress-testing-library cannot query inside iframes without special handling. The button inside an iframe is not accessible from the main document context, causing findByRole to fail. Role attributes are inferred from HTML elements, so B is unlikely. The name option is case-insensitive. cy.wait() is rarely needed if the page is fully loaded.
❓ framework
expert2:30remaining
Which cypress-testing-library command chain correctly waits for an async element to appear before asserting?
You want to wait for a loading spinner to disappear and then check that a 'Data Loaded' message appears. Which command chain is correct?
Attempts:
2 left
💡 Hint
Use chaining and then() to sequence assertions after async waits.
✗ Incorrect
Option B correctly waits for the spinner to disappear, then in the .then() callback queries for the 'Data Loaded' text and asserts it exists. Option B runs assertions sequentially but does not guarantee spinner is gone before checking text. Option B tries to chain findByText on the result of should('not.exist'), which is invalid. Option B uses arbitrary wait which is unreliable.