One possible approach, though not perfect, could use some enhancements...
The Countdown site does not work well within an iframe, but it can still be tested in a child window using a custom command described here
Cypress.Commands.add('openWindow', (url, features) => {
const w = Cypress.config('viewportWidth')
const h = Cypress.config('viewportHeight')
if (!features) {
features = `width=${w}, height=${h}`
}
console.log('openWindow %s "%s"', url, features)
return new Promise(resolve => {
if (window.top.aut) {
console.log('window exists already')
window.top.aut.close()
}
// https://developer.mozilla.org/en-US/docs/Web/API/Window/open
window.top.aut = window.top.open(url, 'aut', features)
// giving page enough time to load and set "document.domain = localhost"
// so we can access it
setTimeout(() => {
cy.state('document', window.top.aut.document)
cy.state('window', window.top.aut)
resolve()
}, 10000)
})
})
You can test it like this:
cy.openWindow('https://shop.countdown.co.nz/').then(() => {
cy.contains('Recipes').click()
cy.contains('Saved Recipes', {timeout:10000}) // check for navigation completion
})
I adjusted the setTimeout()
in the custom command to 10 seconds, as the site tends to load slowly.
Configuration:
// cypress.json
{
"baseUrl": "https://shop.countdown.co.nz/",
"chromeWebSecurity": false,
"defaultCommandTimeout": 20000 // see below for better way
}
https://i.sstatic.net/8khPM.png
Error with Command Timeout
When using Gleb's child window command, there is a timeout error that seems difficult to pinpoint.
To overcome this, I included
"defaultCommandTimeout": 20000
in the configuration, but a more efficient approach would be to remove the global setting and utilize this method instead:
cy.then({timeout:20000}, () => {
cy.openWindow('https://shop.countdown.co.nz/', {}).then(() => {
cy.contains('Recipes').click()
cy.contains('Saved Recipes', {timeout:10000})
})
})
To verify that the extended command timeout only applies once, intentionally delay one of the inner test commands and confirm it times out after the standard 4000 ms timeframe.
cy.then({timeout:20000}, () => {
cy.openWindow('https://shop.countdown.co.nz/', {}).then(() => {
cy.contains('Will not find this').click() // Timed out retrying after 4000ms