Reputation: 915
I am trying to find a solution to access to an iframe into an iframe using cypress. I have already tried all the following function without success:
Cypress.Commands.add('getIframeBody', (iframeSelector) => {
// get the iframe > document > body
// and retry until the body element is not empty
return cy
.get(iframeSelector)
.its('0.contentDocument.body').should('not.be.empty')
// wraps "body" DOM element to allow
// chaining more Cypress commands, like ".find(...)"
// https://on.cypress.io/wrap
.then(cy.wrap)
})
Cypress.Commands.add('iframev3', { prevSubject: 'element' }, (iframeSelector, callback) => {
// For more info on targeting inside iframes refer to this GitHub issue:
// https://github.com/cypress-io/cypress/issues/136
cy.log('Getting iframe body')
return cy
.get(iframeSelector)
.wrap($iframe)
.should(iframe => expect(iframe.contents().find('body')).to.exist)
.then(iframe => cy.wrap(iframe.contents().find('body')))
.within({}, callback)
})
Cypress.Commands.add('iframeV4', { prevSubject: 'element' }, $iframe => {
return new Cypress.Promise(resolve => {
$iframe.ready(function() {
resolve($iframe.contents().find('body'));
});
});
});
I have also tried the iframe plugin.
And I try to access it with this method for example:
cy.get('#m_c_layoutElem_cmsdesktop').iframeV4()
.get('#m_c_layoutElem_contentview', {timeout: 10 * 1000}).iframeV4().debug()
But it is always the same I am able to grab the first iframe but not the second one!
REgards,
EDIT: Thanks to the current comments I have advance a little bit and here is the current state:
cy.getIframeBody('#m_c_layoutElem_cmsdesktop')
.then(iframe1 => {
let iframe2 = iframe1.find('#m_c_layoutElem_contentview')[0] //Do stuff with queryselector()
console.log(iframe2);
let iframe3 = iframe2.contentWindow.document.querySelector('#m_c_plc_lt_ctl00_HorizontalTabs_l_c');
console.log('iframe3', iframe3) // This is set to an iframe
console.log(iframe3.contentWindow) //This is null
//debugger
/*let cyIframe3 = Cypress.$(iframe3.contentWindow.document);
console.log('cy iframe3', cyIframe3);
*/
let cyIframe32 = cy.get(iframe3);
console.log('cy iframe32', cyIframe32); //This is: $Chainer {userInvocationStack: " at Context.eval (http://localhost:8090/__cypress/tests?p=cypress\support\index.js:237:25)", specWindow: Window, chainerId: "chainer1656", firstCall: false, useInitialStack: false}
/*let field = iframe3.contentWindow.document.querySelector('#m$c$f$Title$txtText'); // Exception because iframe3.contentWindow is null
console.log(field);
*/
iframe3.find('#m$c$f$Title$txtText').type('Coucou'); //fin is not a function
});
Upvotes: 3
Views: 7299
Reputation: 675
It is very simple. There are two ways. (Following are Real reproducible examples)
it("Using 'contentDocument' property and its() - Nested iFrames", function() {
cy.visit("https://www.play1.automationcamp.ir/frames.html")
cy.get("#frame1")
.its('0.contentDocument')
.its('body')
.find("#frame2")
.its('0.contentDocument')
.its('body')
.find("#click_me_2").click()
})
it("Using yielded jQuery element - Nested iFrames", function() {
cy.visit("https://www.play1.automationcamp.ir/frames.html")
cy.get("#frame1").then(function($iFrame1){
const iframe2 = $iFrame1.contents().find('#frame2')
cy.wrap(iframe2).as('iframe2Ref')
cy.get('@iframe2Ref').then(function($iFrame2){
const iFrame2Contents = $iFrame2.contents().find('body')
cy.wrap(iFrame2Contents).find("#click_me_2").click()
})
})
})
Upvotes: 1
Reputation: 71
Don't know if this is going to help you or if you already fixed it, but I did find a solution to access an iframe inside another iframe, cause I was trying to solve that issue for myself, will leave the answer here and hope it can help.
So an example of the id of the iframes:
Here is the code sample you can try:
cy.get('#first_iframe').then($firstIframe => {
const $secondIframeReference = $firstIframe.contents().find('#second_iframe');
cy.wrap($secondIframeReference).as('secondIframeReference'); // Saving this as a reference so we can access it again using get command
// Now we are accessing the second iframe
cy.get('@secondIframeReference').then($secondIframe => {
const $elementYouWant = $secondIframe.contents().find('element you want to interact with');
cy.wrap('$elementYouWant').type("It's writing some stuff"); // In case it is an input field, for example
});
});
Got into this solution after reading this article
Upvotes: 7