Cypress iFrame Interaction Error Fix
In this tutorial, you'll learn about Cypress iFrame Interaction Error Fix. We cover key concepts, practical examples, and best practices.
You try cy.get('#my-frame').find('.submit-btn') inside an iframe and Cypress throws — Cypress cannot directly access elements inside cross-origin iframes due to same-origin policy.
The Problem
// WRONG — direct access to iframe content
cy.get('#payment-frame')
.its('0.contentDocument.body')
.find('#card-number')
.type('4242424242424242');
CypressError: Cypress detected a cross-origin error.
Blocked a frame with origin "http://localhost:3000" from accessing a cross-origin frame.
Cypress runs inside the top-level window. Cross-origin iframes are subject to browser security — contentDocument is null for cross-origin frames.
Step-by-Step Fix
1. Use cypress-iframe plugin
npm install cypress-iframe --save-dev
// cypress/support/e2e.js
import 'cypress-iframe';
// test.js
describe('Payment iframe', () => {
it('fills card details', () => {
cy.visit('/checkout');
cy.frameLoaded('#payment-frame');
cy.iframe('#payment-frame')
.find('#card-number')
.type('4242424242424242');
cy.iframe('#payment-frame')
.find('#expiry')
.type('12/28');
cy.iframe('#payment-frame')
.find('#cvv')
.type('123');
});
});
2. Use cy.origin for cross-origin iframes (Cypress 12+)
// RIGHT — use cy.origin for cross-origin iframe interaction
cy.origin('https://payments.stripe.com', () => {
cy.get('#card-number').type('4242424242424242');
cy.get('#expiry').type('12/28');
cy.get('#cvv').type('123');
});
cy.origin() changes Cypress's execution context to the target origin, allowing interaction with iframe elements as if they were in the main window.
3. Bypass by loading the iframe URL directly
// RIGHT — test the iframe content directly
cy.visit('https://payments.stripe.com/payment-page');
cy.get('#card-number').type('4242424242424242');
cy.get('#submit').click();
This bypasses the iframe entirely and tests the payment page in isolation. Useful when the iframe embeds a known third-party service.
Expected output:
✓ fills card details (3456 ms)
✓ tests iframe origin directly (2100 ms)
Prevention Tips
- Use
<a href="/testing-qa/cypress/">cypress</a>-iframeplugin for iframe-heavy tests - Prefer
cy.origin()for cross-origin frames in Cypress 12+ - Test iframe content independently when possible
- Avoid nested iframe chains — they get exponentially harder
- Use
cy.frameLoaded()to wait for iframe to load before interacting
Common Mistakes with iframe error
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world CYPRESS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro