Skip to content

Instantly share code, notes, and snippets.

@bahrmichael
Created September 2, 2019 17:17
Show Gist options
  • Save bahrmichael/401b6c37ed3191879a6292977ba09470 to your computer and use it in GitHub Desktop.
Save bahrmichael/401b6c37ed3191879a6292977ba09470 to your computer and use it in GitHub Desktop.
Cypress.Commands.add(
'iframeLoaded',
{prevSubject: 'element'},
($iframe) => {
const contentWindow = $iframe.prop('contentWindow');
return new Promise(resolve => {
if (
contentWindow &&
contentWindow.document.readyState === 'complete'
) {
resolve(contentWindow)
} else {
$iframe.on('load', () => {
resolve(contentWindow)
})
}
})
});
Cypress.Commands.add(
'getInDocument',
{prevSubject: 'document'},
(document, selector) => Cypress.$(selector, document)
);
Cypress.Commands.add(
'getWithinIframe',
(targetElement) => cy.get('iframe').iframeLoaded().its('document').getInDocument(targetElement)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment