In Cypress, you need to install a package called cypress-iframe
in order to work on iFrame elements.
Run this command in your terminal to install:
npm install -D cypress-iframe
Import cypress-iframe
package to your file:
import 'cypress-iframe';
To load an iFrame, use:
cy.frameLoaded("#id-of-an-iframe");
To find element inside iFrame, start with:
cy.iframe()
Here is the code example on how to use iFrame elements in Cypress:
/// <reference types="Cypress" />
/// <reference types="cypress-iframe" />
import 'cypress-iframe';
describe("Test Suite", () => {
it("Test Case", () => {
cy.visit("https://example.com");
cy.frameLoaded("#iframe-id"); //Load iFrame
cy.iframe().find("a[href*='services']").eq(0).click(); //click on link inside iFrame
cy.iframe().find("h1[class*='service-title']").should("have.length", 2); //assertion
})
})