Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocking window.print #894

Closed
paulpruteanu opened this issue Nov 9, 2017 · 9 comments
Closed

mocking window.print #894

paulpruteanu opened this issue Nov 9, 2017 · 9 comments

Comments

@paulpruteanu
Copy link

Had anyone tried mocking window.print?

I have a scenario where I need to click on a button that window.print's something, in order to move to the next step, and the test remains stuck in the browser print dialog box at the moment.

I tried with

cy.visit(url, { onBeforeLoad: win => cy.stub(win, 'print').returns(undefined) })

also with

cy.window(win => cy.stub(win, 'print').returns(undefined))

but each time I click on the printing button, I'm prompted with the browser print preview box.

Thank you!

@brian-mann
Copy link
Member

Stubbing print definitely works. Put a debugger in your app code right before its about to call window.print and make sure its the stub as opposed to the native function.

As long as its stubbed it will not call through to the native function.

You're either stubbing the function too late (so after your app calls it), or you're doing something like transitioning to another page which then loses the stub.

You can use this event to always stub it on every page transition: https://docs.cypress.io/api/events/catalog-of-events.html#Window-Before-Load

@brian-mann
Copy link
Member

it('can stub print', function () {
  cy.visit('/inner.html', {
    onBeforeLoad: (win) => {
      cy.stub(win, 'print')
    }
  })

  cy.window().then((win) => {
    win.print()

    expect(win.print).to.be.calledOnce
  })
})

screen shot 2017-11-09 at 9 36 18 am

@brian-mann
Copy link
Member

Closing this as this is a question, not a bug.

@paulpruteanu
Copy link
Author

confirmed, sorry for the noise J

@brian-mann
Copy link
Member

No problem. For the record you can stub anything so long as it is set to { configurable: true } when you do this:

Object.getOwnPropertyDescriptor(obj, 'prop')

If its set to false you cannot stub it - and the best practice in that case is to wrap that API in your own function and just stub that function.

For instance you cannot stub window.location API's.

So in that case

const myObj = {}

myObj.href = function (url) {
  window.location.href = url
}
// in cypress code
cy.stub(myObj, 'href')

@sagarrajput
Copy link
Contributor

sagarrajput commented Oct 1, 2021

Hello @brian-mann
I trying to stub print preview but getting, could you please help, Ultimately I want to close print preview or somehow avoid from opening,
expected print to have been called exactly once, but it was never called

My code looks like
cy.window().then(win => {
cy.stub(win, 'print').as('print');
});
cy.get('my locator').click(); // this click event opens print widdow(Only after closing print preview window my API call happens).
cy.get('@print').should('have.been.calledOnce');

@yogithesymbian
Copy link

https://docs.cypress.io/api/events/catalog-of-events.html#Window-Before-Load

engine :

Electron 94

but force close and print is still stuck ( not cancel / close )
Screenshot 2023-12-29 at 3 57 41 PM

        cy.get("div:nth-of-type(8) div.space-y-4 > div.flex > button").click();
        cy.wait(3000);
        cy.window().then((win) => {
          win.print()

          expect(win.print).to.be.calledOnce
        });

force close

libc++abi: Pure virtual function called!
The Test Runner unexpectedly exited via a exit event with signal SIGABRT

Please search Cypress documentation for possible solutions:

https://on.cypress.io

Check if there is a GitHub issue describing this crash:

https://github.com/cypress-io/cypress/issues

Consider opening a new issue.

----------

Platform: darwin-x64 (23.2.0)
Cypress Version: 9.5.2

@rakeshnambiar
Copy link

@yogithesymbian Did you manage to solve it? I am also in a similar situation perhaps I wanted to print the content in PDF format and I don't care about closing the popup. If you have a code snippet, feel free to share it.

@rakeshnambiar
Copy link

Hello all, I wanted to save it as a PDF from the print popup. Please can someone send me a code snippet to solve it? Thanks a lot.
Screenshot 2024-10-31 at 14 48 47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants