Window close

📺 Watch this recipe explained in the video Testing window.close Method From Cypressopen in new window.

<div>Action saved, you can close this window</div>
<button id="closeit">Close</button>
<script>
  document
    .getElementById('closeit')
    .addEventListener('click', () => {
      window.close()
    })
</script>

First, stub the window.close method the button is expected to call.

cy.window().then((win) => {
  cy.stub(win, 'close').as('close')
})

Next, click on the button.

cy.contains('button', 'Close').click()

Finally, confirm the window.close method was called by the application.

cy.get('@close').should('have.been.calledOnce')

See also