question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mocking window.print

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
brian-manncommented, Nov 9, 2017
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

0reactions
sagarrajputcommented, 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’);

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I mock the JavaScript 'window' object using Jest?
I would like to mock window's open function, so I can verify the correct URL is passed in to the open function. Using...
Read more >
Mocking window object with Jest - Marek Rozmus - Medium
Mocking window object with Jest” is published by Marek Rozmus. ... First testing the pathname check — new window should be opened when...
Read more >
Mocking the window object in unit tests - DEV Community ‍ ‍
Hi there! Let's talk about how to properly mock that pesky browser window object in a unit testing environment.
Read more >
[Solved]-jest: window.print not implemented-Vue.js
Since window.alert, print and several other browser-specific side effects needs to be stubbed manually. This should preferably be done with Jest, ...
Read more >
How to mock global window variables with Jest
While generally everything is extremely easy using Jest, mocking global variables is one thing which is left up to developer. You can…
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found