admin管理员组

文章数量:1403493

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?

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?

Share Improve this question edited May 14, 2015 at 8:08 alecxe 474k127 gold badges1.1k silver badges1.2k bronze badges asked May 14, 2015 at 8:02 Naveen KumarNaveen Kumar 3,0004 gold badges22 silver badges24 bronze badges 2
  • You can with the help of Sikuli JAVA API in selenium. Please check this my post and also my answer: stackoverflow./questions/23213535/… – nitin chawda Commented May 14, 2015 at 8: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 Commented May 14, 2015 at 8:32
Add a ment  | 

1 Answer 1

Reset to default 8

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:

  • Handling Print Dialog Using Protractor
  • Selenium WebDriver : Verify Print Window dialog displayed on the page
  • How to handle print dialog in Selenium?

本文标签: javascriptProtractor How to test windowprint()Stack Overflow