在我的应用程序中有一个推荐列表,点击后会打开一个带有动态地址的新窗口:
$window.open(_shopURL, '_blank');
现在我试图将windows.open事件存根,如https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/stubbing-spying__window/cypress/integration/window-stubbing.spec.js所示
Cypress.on('window:before:load', (win) => {
win.open = cy.stub().as('windowOpen')
})
describe('Shop integration', () => {
beforeEach(function () {
cy.visitHome(countryCode, resellerId)
})
it('can stub the window open event', function () {
cy.get(`.recommendations-list .recommendations-cover:nth-of-type(1)`)
.click()
cy.get('@windowOpen').should('be.calledWith', 'page1.html')
})
但它始终打开新选项卡并且日志错误:Cypress: stub open window
有没有人知道它为什么不起作用?干杯!
我正在为我要测试的每个页面使用页面对象。因此,在我的父页面对象中,每个其他PO继承后,我在打开网址时执行以下操作:
public navigateTo(url: string, defaultTimeout: number = 5000) {
return cy.visit(url, {
onBeforeLoad: (win: any) => {
cy.stub(win, 'open');
},
timeout: defaultTimeOut
});
}
这可以防止窗口打开新页面。