首页 > 解决方案 > 赛普拉斯:如何处理从 iframe 触发的窗口确认关闭

问题描述

我需要有关如何关闭从 iframe 触发的确认窗口的帮助。我正在尝试下面的代码,但这仍然无法关闭确认窗口。

 cy.get('iframe[id="ParentMainContent_Frame"]').then(($iframe) => {
        const $body = $iframe.contents().find('body')
        const $win = $iframe[0].contentWindow

        cy.stub($win,'confirm').as('windowConfirm')
        cy.wrap($body)
        .find('#ParentMainContent_MainContentMaster_ctl00_PlaceOrderButton').click().should(function () {
            expect(this.windowConfirm).to.be.calledWith('Thank you for your Order!')
        })
    }) 

DOM

触发此窗口的按钮如上所示:

希望有人可以看看和帮助。

标签: javascriptiframecypress

解决方案


使用cypress-iframe可以让您在开始测试其内容之前更多地检查 iframe 加载是否已完成。

缺点是这个库的方法 yieldbody但不是 iframe 窗口,尽管我认为你可以使用body.ownerDocument.window它来获取它。

cy.enter('iframe[id="ParentMainContent_Frame"]').then(getBody => {

  const body = getBody();
  const win = body.ownerDocument.window;

  cy.spy(win,'confirm').as('windowConfirm');   // spy not stub

  body.find('#ParentMainContent_MainContentMaster_ctl00_PlaceOrderButton')
    .click()
    .should(function () {
      expect(this.windowConfirm).to.be.calledWith('Thank you for your Order!')
    })
  }) 
}) 

推荐阅读