【问题标题】:How can I close a open Print window in cypress using javascript function如何使用 javascript 函数在 cypress 中关闭打开的打印窗口
【发布时间】:2023-03-28 09:28:01
【问题描述】:

如何在点击 cypress 中的打印按钮时关闭 print window 打开。我正在调用js 函数来关闭窗口并且它没有执行关闭,有人可以建议如何解决这个问题吗?

context('Print button tests', () => {
  it.only('Verify the display of Print and close the window', () => {
    cy.get('.timetable-filter-panel-button.btn.btn-primary > span')
      .contains("Print current view")
      .click();
    printClose();    
  })
})

//关闭打开的窗口的Javascript函数:

function printClose(){
    window.close(); 
}

【问题讨论】:

  • @bkucera 你能帮忙解决上述问题吗?
  • AFAIK,你不能,就像你不能在other browser dialogs上操作一样
  • 找到这个github.com/cypress-io/cypress/issues/894,唯一的解决办法似乎是这样的。
  • 但是我看到了那个解决方案,我想在打开后关闭打印对话框。
  • @soccerway 也许我来晚了,但我遇到了同样的问题,我用下面发布的代码 sn-p 解决了。希望对你有帮助!

标签: javascript cypress


【解决方案1】:

您使用 Cypress 测试打印弹出窗口,如下所示:

it('Print button renders & opens modal', () => {
  // Clicks & asserts the button that triggers the popup window to print
  cy.get('[data-test="button-print"]')
    .should('exist')
    .and('be.visible')
    .click();

  // Assert printing was called once
  cy.visit('/inner.html', {
    onBeforeLoad: win => {
      cy.stub(win, 'print');
    },
  });

  cy.window().then(win => {
    win.print();
    expect(win.print).to.be.calledOnce;

    // this line closes the print popup
    cy.document().type({ esc });
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 2022-10-24
    • 1970-01-01
    • 2018-05-02
    相关资源
    最近更新 更多