0
0
Cypresstesting~10 mins

Iframe interaction strategies in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates how to interact with an iframe using Cypress. It verifies that a button inside the iframe can be clicked and that the expected text appears after the click.

Test Code - Cypress
Cypress
describe('Iframe interaction test', () => {
  it('Clicks button inside iframe and verifies text', () => {
    cy.visit('https://example.com/page-with-iframe')
    cy.get('iframe#iframe-id').then($iframe => {
      const iframeBody = $iframe.contents().find('body')
      cy.wrap(iframeBody).find('button#inside-iframe-btn').click()
      cy.wrap(iframeBody).find('p#result-text').should('contain.text', 'Button clicked!')
    })
  })
})
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test startsCypress test runner initialized-PASS
2Browser opens and navigates to 'https://example.com/page-with-iframe'Page with iframe loadedURL is correctPASS
3Find iframe element with selector 'iframe#iframe-id'Iframe element found in DOMIframe element existsPASS
4Access iframe's document body using jQuery contents() and find('body')Iframe's body element accessedIframe body is accessiblePASS
5Wrap iframe body and find button with selector 'button#inside-iframe-btn'Button inside iframe foundButton element exists inside iframePASS
6Click the button inside the iframeButton clicked, iframe content updated-PASS
7Find paragraph with selector 'p#result-text' inside iframe and check text contentParagraph element found inside iframeText contains 'Button clicked!'PASS
8Test endsTest completed successfully-PASS
Failure Scenario
Failing Condition: The iframe or button inside it is not found or the expected text does not appear after clicking the button
Execution Trace Quiz - 3 Questions
Test your understanding
What is the correct way to access the body of an iframe in Cypress?
AUse cy.get('iframe').then($iframe => $iframe.contents().find('body'))
BUse cy.get('iframe').click() directly
CUse cy.get('iframe').find('button') without accessing contents()
DUse cy.get('body') to access iframe content
Key Result
When testing iframes with Cypress, always access the iframe's document body using jQuery contents() and wrap it with cy.wrap() to chain Cypress commands inside the iframe context.