|
/* cypress/support/command.js */ |
|
let originalWindow = null; |
|
|
|
Cypress.Commands.add('openWindow', (url, features) => { |
|
if(!originalWindow){ |
|
originalWindow = cy.state('window'); |
|
originalWindow.APP_ID = 1; // depth 1 |
|
} |
|
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.MyAltWindow && window.top.MyAltWindow.close) { |
|
console.log('window exists already') |
|
window.top.MyAltWindow.close() |
|
} |
|
// https://developer.mozilla.org/en-US/docs/Web/API/Window/open |
|
window.top.MyAltWindow = window.top.open(url, 'MyAltWindow', features) |
|
window.top.MyAltWindow.APP_ID = 2; // TODO: make this support n-many |
|
|
|
// letting page enough time to load and set "document.domain = localhost" |
|
// so we can access it |
|
setTimeout(() => { |
|
cy.state('document', window.top.MyAltWindow.document) |
|
cy.state('window', window.top.MyAltWindow) |
|
resolve() |
|
}, 500) |
|
}) |
|
}) |
|
|
|
/* toggle between 2 for now, could set this up to handle N-many windows */ |
|
Cypress.Commands.add('toggleWindows', ()=>{ |
|
return new Promise(resolve=>{ |
|
if(cy.state('window').APP_ID === 1){ |
|
// switch to our ALT window |
|
console.log('switching to alt popup window...') |
|
cy.state('document', originalWindow.top.MyAltWindow.document) |
|
cy.state('window', originalWindow.top.MyAltWindow) |
|
originalWindow.blur() |
|
}else{ |
|
console.log('switching back to original window') |
|
// switch back to originalWindow |
|
cy.state('document', originalWindow.document) |
|
cy.state('window', originalWindow) |
|
originalWindow.top.MyAltWindow.blur() |
|
} |
|
window.blur(); |
|
|
|
cy.state('window').focus() |
|
|
|
resolve(); |
|
}) |
|
}) |
|
|
|
Cypress.Commands.add('closeWindow', ()=>{ |
|
return new Promise(resolve=>{ |
|
if(window.top.MyAltWindow && window.top.MyAltWindow.close){ |
|
window.top.MyAltWindow.close() // close popup |
|
window.top.MyAltWindow = null |
|
} |
|
if(originalWindow){ |
|
cy.state('document', originalWindow.document) |
|
cy.state('window', originalWindow) |
|
} |
|
cy.state('window').focus() |
|
resolve() |
|
}) |
|
}) |
my issue is about switch window scenario is below
my application there is have the button upload ,when i click this button there opened a now window ,and then first window window.name ='',new window name is have defined by dev like pre-screen ,the url is the same url just window name is diffreret ,i want to go the new window and get element or some opreation ,but now i can't enter the new window that you provied solution,could you please give me other solution fix this ?