Stub window.print

You can find this recipe shown in the video Stub window.print Methodopen in new window.

App calls the window.print method

<p>
  Click on the button to print this page
  <button id="print">🖨</button>
</p>
<script>
  document
    .getElementById('print')
    .addEventListener('click', () => {
      window.print()
    })
</script>
cy.window().then((w) => {
  cy.stub(w, 'print').as('print')
})
cy.get('#print').click()
cy.get('@print').should('be.calledOnce')

Onclick button attribute

<p>
  Click on the button to print this page
  <button id="print" onclick="print()">🖨</button>
</p>
cy.window().then((w) => {
  cy.stub(w, 'print').as('print')
})
cy.get('#print').click()
cy.get('@print').should('be.calledOnce')