2

I'm trying to test print functionality of a button, like:

it('print document', function(){
    element(by.id('print-button')).click();
    expect(window.print());
});

I want to test browser print dialog box. How to do this?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Naveen Kumar
  • 2,570
  • 3
  • 20
  • 22
  • You can with the help of Sikuli JAVA API in selenium. Please check this my post and also my answer: http://stackoverflow.com/questions/23213535/how-to-test-print-preview-of-chrome-browser-using-chrome-webdriver – nitin chawda May 14 '15 at 08:32
  • @nitinchawda yup, but this is not java or python, so Sikuli is probably not the best option. Plus, the solution would be not quite reliable since it would depend on a specific browser, hence a specific image of a print dialog. – alecxe May 14 '15 at 08:32

1 Answers1

7

Browser's print dialog is out of scope of selenium, it is not under selenium's control. There is no way to solve your problem reliably with protractor/selenium only.

Besides, you don't need to test the browser and it's ability to open print dialogs. What you can do (not tested), is to test whether window.print is called on print-button click by redefining window.print() (reference):

browser.setScriptTimeout(10);

var printButton = element(by.id('print-button'));

var result = browser.executeAsyncScript(function (elm, callback) {
    function listener() {
        callback(true);
    }

    window.print = listener;
    elm.click();
}, printButton.getWebElement());

expect(result).toBe(true);

See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195