I am looking to perform action on print window using cypress.
Manually when I click on the print button I get the print window like Image 1. But when I run the same script in cypress test runner, I get the print windows as in Image 2.
I am aware that action on such window can be done using a stub. But I cant even inspect print window I get in test runner i.e. Image 2. so what shall be the locator of cancel button ??
let printStub
cy.window().then(win => {
printStub = cy.stub(win, 'print')
cy.get(HOW DO I GET THE LOCATOR).click().then(()=>{
expect(win.print).to.be.called
})
})
I'm not sure what causes the difference in the two print windows, but tecnically you don't need to test the actually printing part, just that window.print has been called.
If you agree, then follow this example Replace a method with a function, adding a dummy function as 3rd parameter to the stub.
let printCalled = false
cy.stub(cy.state('window'), 'print', () => {
printCalled = true
})
cy.get('the-print-button-selector').click() // app calls window.print
.should(() => expect(printCalled).to.eq(true))
or just assert the call happened
const stub = cy.stub(cy.state('window'), 'print', () => {}) // replace print with do-nothing fn
cy.get('the-print-button-selector').click() // app calls window.print
.should(() => expect(stub).to.be.called)