Challenge - 5 Problems
Iframe Interaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Cypress iframe interaction code
What will be the output of this Cypress test code snippet when it tries to get the text inside an iframe's body?
Cypress
cy.get('iframe#myFrame').its('0.contentDocument.body').should('not.be.empty').then(cy.wrap).invoke('text').then(text => cy.log(text))
Attempts:
2 left
💡 Hint
Remember that Cypress can access iframe contentDocument if same-origin policy allows.
✗ Incorrect
The code accesses the iframe's body element, wraps it for Cypress commands, then invokes 'text' to get the text content and logs it. This works if the iframe is loaded and same-origin policy is satisfied.
❓ assertion
intermediate2:00remaining
Correct assertion for iframe content visibility
Which assertion correctly verifies that an element with id 'submitBtn' inside an iframe with id 'loginFrame' is visible using Cypress?
Attempts:
2 left
💡 Hint
You need to access the iframe's document body before finding elements inside it.
✗ Incorrect
Option C correctly accesses the iframe's contentDocument body, then finds the element inside it and asserts visibility. Other options either miss accessing iframe content or use invalid chaining.
🔧 Debug
advanced2:00remaining
Debugging iframe element not found error
You have this Cypress code to click a button inside an iframe, but it fails with 'element not found' error. What is the most likely cause?
Cypress
cy.get('iframe#paymentFrame').its('0.contentDocument.body').find('button#payNow').click()
Attempts:
2 left
💡 Hint
Consider timing and loading of iframe content before accessing elements inside.
✗ Incorrect
The most common cause is that the iframe content is not fully loaded when Cypress tries to find the button, causing element not found error. Waiting or retrying is needed.
❓ framework
advanced2:00remaining
Best Cypress approach for interacting with cross-origin iframes
Which approach is recommended to interact with elements inside a cross-origin iframe in Cypress?
Attempts:
2 left
💡 Hint
Cypress added special support for cross-origin testing recently.
✗ Incorrect
Cypress's cy.origin() command allows running commands inside a different origin context, enabling interaction with cross-origin iframes safely and correctly.
🧠 Conceptual
expert2:00remaining
Why direct iframe DOM manipulation is discouraged in Cypress
Why is directly manipulating iframe DOM elements using native JavaScript (e.g., contentDocument.body.innerHTML = '') discouraged in Cypress tests?
Attempts:
2 left
💡 Hint
Think about how Cypress manages commands and retries.
✗ Incorrect
Direct DOM manipulation bypasses Cypress's command queue and automatic retries, which can cause tests to be flaky and unreliable. Using Cypress commands ensures proper synchronization.