How do you work with iframes, new tabs, and file downloads and uploads?
Cypress works in one tab, so each of the four scenarios has its own approach. The content of a same-origin iframe is reached through contentDocument.body wrapped in cy.wrap, while an iframe from another domain is blocked by the same-origin policy. New tabs are not opened: the target attribute is removed or href is checked. Uploads use .selectFile(), and a downloaded file is checked with cy.readFile.
Cypress · Locators and DOM
A payment form sits in an iframe and a link opens in a new tab. How do you test that in Cypress?
Four scenarios raise the most questions: iframes, new tabs, uploads and downloads. Cypress works in one browser tab and by default has no dedicated iframe command, so each scenario has its own approach and its own limits.
How it works and why. The test runs in the same browser as the app, but in one tab. An iframe is a separate document: cy.get('iframe') finds the element itself, and its content is reachable through contentDocument.body, which is wrapped in cy.wrap; the cypress-iframe plugin adds convenience. A cross-origin iframe is blocked by the same-origin policy; in Chromium browsers this can be relaxed with chromeWebSecurity: false, in Firefox it cannot, and cy.origin does not cover iframes. New tabs are not opened by Cypress itself: remove target from the link or check href (a real second tab needs the official @cypress/puppeteer plugin). Upload is the built-in .selectFile() command. A download is stored by the browser in the downloadsFolder (by default cypress/downloads), where you check the file with cy.readFile, which retries until the file appears.
What to use for each scenario
Same origin
An iframe from your domain opens through contentDocument.body and cy.wrap.
Another origin
Third-party payment widgets are not directly reachable; test the integration (request, redirect), not their form.
Example: four scenarios in one file.
// iframe of the same origin
cy.get('iframe#payment')
.its('0.contentDocument.body').should('not.be.empty')
.then(cy.wrap)
.find('[data-cy=card]').type('4242424242424242');
// new tab: open it in the same one
cy.get('[data-cy=docs-link]').invoke('removeAttr', 'target').click();
// or only check the requirement on the link
cy.get('[data-cy=docs-link]')
.should('have.attr', 'target', '_blank')
.and('have.attr', 'href', '/docs');
// upload: a path, an alias or a Buffer
cy.get('input[type=file]').selectFile('cypress/fixtures/avatar.png');
cy.get('[data-cy=dropzone]').selectFile('cypress/fixtures/avatar.png', {
action: 'drag-drop',
});
// download: readFile retries until the file appears
cy.get('[data-cy=export]').click();
cy.readFile('cypress/downloads/report.csv').should('contain', 'Total');
.selectFile() by default needs a visible element: for a hidden input add { force: true }, which is one of the justified cases. Before a download delete the old file or use a unique name, otherwise the assertion passes on a leftover from a previous run.
Limits and alternatives. To verify that a link opens a tab, stub window.open: cy.window().then((win) => cy.stub(win, 'open').as('open')) and then cy.get('@open').should('be.calledWith', ...). If the app opens a tab and its content must be checked, the official @cypress/puppeteer plugin gives access to it through cy.puppeteer() (Chromium-family browsers only). For scenarios where tabs, third-party iframes or several users in different windows are the core of the test, Cypress is not the best tool.
Playwright is clearly stronger here: iframes via page.frameLocator(), including cross-origin ones, a new tab via context.waitForEvent('page'), upload via setInputFiles(), download via page.waitForEvent('download').
Common mistakes.
- Taking
cy.get('iframe')and calling.findat once: that is the iframe element, not its document. - Not waiting for the iframe to load:
contentDocument.bodyis empty until the document loads, hence thenot.be.emptyassertion. - Looking for the downloaded file in another folder or without cleanup: the test depends on previous runs.
Check yourself
Why does the iframe example contain .should('not.be.empty')?
Answer
Until the iframe loads, its body is empty and find would find nothing. The assertion retries until the document appears, so the test waits for a ready iframe.
How do you verify that a button opens a new tab when Cypress has only one?
Answer
Stub window.open and check the call, or check the target and href attributes of the link. Cypress will not open the tab itself.