简体   繁体   English

赛普拉斯:存根打开 window

[英]Cypress: Stub open window

in my app there is an recommendations list, which on click opens a new window with a dynamic address:在我的应用程序中有一个推荐列表,点击它会打开一个带有动态地址的新 window:

$window.open(_shopURL, '_blank');

Now I'm trying to stub the windows.open event as shown in https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/stubbing-spying__window/cypress/integration/window-stubbing.spec.js现在我正在尝试存根 windows.open 事件,如https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/stubbing-spying__window/cypress/integration/window中所示.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')
})

But it's always opening the new tab and the logs are wrong: Cypress: stub open window但它总是打开新标签并且日志是错误的: Cypress: stub open window

Does anybody has an idea why it's not working?有人知道为什么它不起作用吗? Cheers干杯

I'm using page-objects for every page I want to test.我正在为要测试的每个页面使用页面对象。 So in my parent page-object which gets inherited by every other PO I do the following when opening a url:因此,在我的父页面对象中,每个其他 PO 都继承了我在打开 url 时执行以下操作:

 public navigateTo(url: string, defaultTimeout: number = 5000) { return cy.visit(url, { onBeforeLoad: (win: any) => { cy.stub(win, 'open'); }, timeout: defaultTimeOut }); }

This prevents window to open a new page.这可以防止窗口打开新页面。

Code below will help you to stub window.open and further assert it that function has been triggered:下面的代码将帮助您存根 window.open 并进一步断言该函数已被触发:

it('opens the about page', () => {
  cy.visit('/')
  cy.window().then(win => {
    cy.stub(win, 'open').as('Open')
  })
  cy.get('.your-selector').click()
  cy.get('@Open').should('have.been.calledOnceWithExactly', yourUrl)
})

You also can stub window.open in cy.on hook as you did, what helps you to yield new window object each time after page reload.您也可以像您一样在cy.on钩子中存根window.open ,这有助于您每次在页面重新加载后生成新的窗口对象。 However, if you want to actually open the new Url in existing tab instead of new one you can use this code below by passing "_self" param to overwrite old "_blank" :但是,如果您想在现有选项卡中实际打开新的 Url 而不是新的,您可以使用下面的代码,通过传递"_self"参数来覆盖旧的"_blank"

cy.window().then(win => {
          cy.stub(win, 'open').callsFake((url) => {
            return win.open.wrappedMethod.call(win, url, '_self');
          }).as('Open');
        });

callsFake function dynamically withdraws url which has been placed into original window.open(url, "_blank"), or you can manually change url inside .call(win, url, '_self'); callFake 函数动态撤回原来放在window.open(url, "_blank")中的url,也可以手动修改.call(win, url, '_self')中的url; with static one, so regardless on which link or button you clicked, which triggers window.open, they all will open the same url.使用静态的,因此无论您单击哪个链接或按钮,触发 window.open,它们都将打开相同的 url。

You also can use this easy way:您也可以使用这种简单的方法:

const newUrl = 'your url';

cy.window().then((win) => {
    cy.stub(win, 'open').callsFake(url => {
        newUrl = url
    }).as('windowOpen')
})

cy.get('your path').click()
cy.get('@windowOpen').should('be.called')

cy.visit(newUrl)   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM