在我的应用程序中有一个推荐列表,单击该列表会打开一个带有动态地址的新窗口:
$window.open(_shopURL, '_blank');
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试存根 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')
})
Run Code Online (Sandbox Code Playgroud)
但它总是打开新选项卡并且日志是错误的: Cypress:stub open window
有人知道为什么它不起作用吗?干杯!