Skip to content

Instantly share code, notes, and snippets.

@iqbmo04
Forked from subtleGradient/setWindow.cypress.js
Created July 6, 2021 23:00
Show Gist options
  • Select an option

  • Save iqbmo04/d14faefa1916186120772cfb91cb6cda to your computer and use it in GitHub Desktop.

Select an option

Save iqbmo04/d14faefa1916186120772cfb91cb6cda to your computer and use it in GitHub Desktop.
Test multiple popup windows and iframes with Cypress, including window.open
/*
The MIT License
Copyright 2020 Thomas Aylott <oblivious@subtlegradient.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/// <reference types="cypress" />
Cypress.Commands.add(
'setWindow',
(/** @type {Window}*/ window, timeout = 500) =>
// may or may not need to set document.domain
new Promise((resolve) => {
setTimeout(() => {
cy.state('window', window);
cy.state('document', window.document);
resolve();
}, timeout);
})
);
Cypress.Commands.add('shouldSetWindow', (/** @type {() => Window}*/ getWindow, timeout = 500) => {
/** @type {Window}*/
let window;
return cy
.shouldRetry(() => {
try {
window = getWindow();
} catch (e) { }
expect(typeof window === 'object').to.be.true;
expect(toString.call(window)).to.equal('[object Window]');
})
.then(() => cy.setWindow(window, timeout))
.log('cy window and document changed');
});
/**
* Workaround to allow passing a timeout to `should`
* `should` keep executing until all expectations inside its callback pass
*/
Cypress.Commands.add('shouldRetry', (callback, options) => cy
.get('html', { log: false, ...options })
.should(callback)
);
Cypress.Commands.add('shouldBeTrueEventually', (/** @type {() => boolean} */assertion, options) =>
cy.shouldRetry(() => expect(assertion()).to.be.true, options)
);
/// <reference types="cypress" />
/** @type {Window}*/
let popupWindow;
/** @type {Window}*/
let grandChildWindow;
afterEach(() => {
if (popupWindow && typeof popupWindow.close === 'function') {
popupWindow.close();
}
if (grandChildWindow && typeof grandChildWindow.close === 'function') {
grandChildWindow.close();
}
});
it('does cool stuff!', () => {
/** @type {Window & { CHILD_WINDOW?: Window }}*/
let mainWindow;
cy.visit('https://double.observer/')
.window().then((/** @type {typeof mainWindow}*/ window) => (mainWindow = window))
cy.get('[alt="the thing that triggers the popup"]').click()
// you can just use shouldSetWindow as if the popup is ready already
// under the hood, it'll keep checking for the existance of the child window
// once it finally finds it, it'll run `cy.setWindow`
.shouldSetWindow(() => (popupWindow = mainWindow.CHILD_WINDOW))
.get('something inside the popup window!').click()
cy.setWindow(mainWindow)
.get('something inside the main window!').click()
cy.setWindow(popupWindow)
.get('something inside the popup window!').click()
cy.shouldSetWindow(() => (grandChildWindow = popupWindow.open('about:blank')))
.get('something inside the grandChildWindow!').click()
cy.setWindow(popupWindow)
.get('something inside the popup window!').click()
cy.setWindow(grandChildWindow)
.get('something inside the popup grandChildWindow!').click()
.then(() => grandChildWindow.close())
cy.setWindow(mainWindow)
.get('something inside the main window!').click()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment