-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
window-open-example.js
41 lines (29 loc) · 1.41 KB
/
window-open-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// this example relies on https://github.com/jakedowns/CypressHelpers/blob/master/commands_tab_helpers.js being added to your support/commands file
cy.visit('/page-with-window-open',{
// NOTE: we define a "tab_name" for the "Root Tab" so we can switch back to it
tab_name: 'myRootWindow',
onBeforeLoad(win) {
// stub window.open with a custom version
cy.stub(win, 'open', (url, target, features)=>{
// our custom window.open that allows cypress to switch between the windows ("called tabs in the helpers")
cy.openTab(url, {
tab_name: 'MySecondWindow', // make this something you can call later to switch Cypress contexts between the parent window and the new window
window_features: features,
})
}).as('windowOpen');
}
})
// cy.click('Open New Window');
cy.get('@windowOpen').should('have.been.calledOnce')
const EXPECTED_WINDOW_OPEN_URL = 'https://mydomain.com/some-other-page';
cy.wrap(window.callMyJSThatCallsWindowOpen()).then(()=>{
cy.get('@windowOpen').should('be.calledWith', EXPECTED_WINDOW_OPEN_URL)
// switch to the new window
cy.switchToTab('MySecondWindow')
// TEST STUFF INSIDE THE SECOND WINDOW
// Close the Tab
cy.closeTab('MySecondWindow')
// Switch back to the root window
// NOTE: you can switch back and forth without closing the tab too
cy.switchToTab('MyRootWindow')
})