Challenge - 5 Problems
Dual Commands Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Cypress dual command chaining?
Consider the following Cypress test code. What will be the text content of the element after the commands run?
Cypress
cy.get('#message').invoke('text').then(text => { cy.get('#message').should('contain.text', text); });
Attempts:
2 left
💡 Hint
Remember that Cypress commands are asynchronous and 'then' waits for the previous command to resolve.
✗ Incorrect
The code gets the text of the element with id 'message' and then asserts that the element contains that text. This is a valid dual command chaining pattern in Cypress.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies the value after a dual command?
You want to check that an input field with id 'email' has the value 'user@example.com' using Cypress dual commands. Which assertion is correct?
Cypress
cy.get('#email').invoke('val').then(value => { // assertion here });
Attempts:
2 left
💡 Hint
Use the most direct equality assertion for exact value matching.
✗ Incorrect
The 'expect' assertion with 'to.equal' checks exact equality, which is appropriate here. 'to.contain' would allow partial matches, which is not precise enough.
🔧 Debug
advanced2:30remaining
Why does this Cypress dual command fail to find the element?
Examine the code below. Why does the test fail with 'Timed out retrying: Expected to find element: #submit, but never found it.'?
Cypress
cy.get('#form').then(form => { cy.wrap(form).find('#submit').click(); });
Attempts:
2 left
💡 Hint
Remember the difference between jQuery elements and Cypress chainables.
✗ Incorrect
Inside 'then', 'form' is a jQuery element, but 'find' is a Cypress command that works on Cypress chainables. Using 'cy.wrap(form).find()' returns a Cypress chainable, but if the element is not found, it times out. The failure is due to timing or selector context.
❓ framework
advanced2:00remaining
How does Cypress handle dual commands internally to avoid race conditions?
Which mechanism does Cypress use internally to ensure dual commands like 'get' and 'then' run in the correct order without race conditions?
Attempts:
2 left
💡 Hint
Think about how Cypress manages command execution flow.
✗ Incorrect
Cypress maintains an internal command queue and runs commands one after another, ensuring each command completes before the next starts. This prevents race conditions and makes test execution predictable.
🧠 Conceptual
expert3:00remaining
What is the main advantage of using dual commands in Cypress tests?
Why do Cypress tests often use dual commands like 'cy.get().then()' instead of just chaining commands directly?
Attempts:
2 left
💡 Hint
Consider how Cypress handles asynchronous commands and user callbacks.
✗ Incorrect
Using 'then' after a Cypress command allows you to work with the yielded subject directly in a callback, enabling custom logic while still benefiting from Cypress's automatic retries and command queue. This is the main advantage of dual commands.