Tabs, iframes, cross-origin
Cypress works in one tab, so it does not open a new tab or window. For tabs the target attribute is removed, or href or a window.open call through a stub is checked, and for a same-origin iframe contentDocument.body is taken and wrapped in cy.wrap. A cross-origin iframe cannot be opened this way because of the same-origin policy, and cy.origin does not help here.
Cypress · Sessions and isolation
A link opens in a new tab and Cypress does not see it. What do you do with tabs and iframes?
The “Terms” button opens a document in a new tab and a payment widget lives in an iframe. The test clicks but nothing happens next or commands do not see the content. Cypress works in one tab and has limits with iframes and other origins. The lesson covers what to do about it without hacks.
How it works and why. A test runs together with the page in one tab, so Cypress does not open or control a new tab or window: commands always go to the test tab. There is no built-in command for an iframe because an iframe is a separate document. Its DOM is reachable through contentDocument if the iframe has the same origin, and for a cross-origin iframe the browser forbids such access (the same-origin policy). So the techniques differ: for tabs you change the link behaviour or check the intent, for iframes you get its body and wrap it in cy.wrap.
Two strategies for a new tab
Remove target
The test removes target="_blank" and the link opens in the same tab. It checks that the user reaches the right page.
Check href or window.open
It checks the link address or the window.open call through a stub. The new tab itself is not needed by the test.
Example: a tab and an iframe.
// 1. open in the same tab
cy.contains('a', 'Terms')
.invoke('removeAttr', 'target')
.click();
cy.url().should('include', '/terms');
// 2. or check the address and follow it
cy.contains('a', 'Terms')
.should('have.attr', 'href')
.then((href) => cy.visit(href as string));
// 3. window.open: stub and check the intent
cy.window().then((win) => cy.stub(win, 'open').as('open'));
cy.contains('button', 'Terms').click();
cy.get('@open').should('have.been.calledWithMatch', '/terms');
// 4. an iframe of the same origin
cy.get('iframe[data-cy=widget]')
.its('0.contentDocument.body')
.should('not.be.empty')
.then(cy.wrap)
.find('[data-cy=pay]')
.click();
The first three variants bypass the new tab: it is not needed for the check. The fourth takes the iframe’s body and works while should('not.be.empty') waits for the document to load. Typically this code goes into a custom command so it is not repeated in every test.
Limits and alternatives. A cross-origin iframe is not opened this way. For Chromium browsers there is the chromeWebSecurity: false option, but it turns off the browser’s protection and changes behaviour, so the test no longer resembles real user work. If you need a real scenario in a foreign iframe, that is a limit of Cypress’s architecture: take Playwright or check such a widget separately from its host. For several tabs the official route is the @cypress/puppeteer plugin: its cy.puppeteer() command works only in Chromium browsers and does not retry. A move to another origin in the same tab (for example, SSO) is solved by cy.origin, but it does not work for iframes.
A new tab is an event: context.waitForEvent('page') or page.waitForEvent('popup'), and an iframe is reached through page.frameLocator(), including cross-origin ones.
Common mistakes.
- Clicking a link with
target="_blank"and expecting changes on the current page: Cypress stayed in the old tab. - Taking
contentDocument.bodywithout waiting: the iframe has not loaded yet andbodyis empty. - Turning off
chromeWebSecurity“just in case”: the test checks behaviour other than what the user sees. - Passing a cross-origin link to
cy.visitwithoutcy.originfor the following commands.
Check yourself
A user clicks “Terms” and the page opens in a new tab. What test checks that the link leads to the right address without a second tab?
Answer
Check the attribute: cy.contains('a', 'Terms').should('have.attr', 'href', '/terms') and, if needed, remove target and follow the link. There is no need to control a second tab.
Why does the code with contentDocument.body not work for an iframe from a payment provider on another origin?
Answer
The browser’s same-origin policy does not let a page read the DOM of an iframe from another origin, so contentDocument is unavailable. Cypress does not change that: another approach is needed (a separate widget test, Playwright) or the undesirable chromeWebSecurity: false.
target="_blank" through invoke('removeAttr', 'target') or check the href. For an iframe you take contentDocument.body and wrap it in cy.wrap. Middle/Senior: I understand this is a limit of the architecture: a cross-origin iframe is not covered by Cypress, several tabs only through the @cypress/puppeteer plugin in Chromium, and cy.origin is only for moving to another origin in the tab. For such scenarios I either check the intent (href, a stub for window.open) or take Playwright.