2

I am doing end-to-end testing with protractor. In a certain test, I need to test like print button is creating pdf or not. So When Test clicks the button, It opens the print window dialog like below.

enter image description here

And now this test is not able to be finished. Because of this print window. My question is how to close this print dialog in protractor? Because of it, rest of test become pending. Please help.

I have tried the following:

var printButton=element(by.css('[class="print"]'));
/* This print button should be present first*/
expect(printButton.isPresent()).toBe(true);

browser.actions().mouseMove(printButton).perform();
printButton.click().then(function () {
     // fill in the form here
     browser.sleep(2000);
    // For Pressing Escape key
    browser.actions().sendKeys(protractor.Key.ESC).perform();

});

I thought If I got successful to press escape key, then It will resolve the issue. But No Success.

Then, I also tried new Windows change like below:

printButton.click().then(function () {
     // fill in the form here

    browser.sleep(4000);
    browser.getAllWindowHandles().then(function(handles){
        browser.switchTo().window(handles[1]).then(function(){
            //do your stuff on the pop up window

            browser.driver.close();
            browser.switchTo().window(handles[0]);
        });
    });

});

but it shows an error in console and actually It does not open any windows. And it is hanging up on print dialog as previous:

Failed: unknown error: failed to close window in 20 seconds

My Last Attempt:

printButton.click().then(function () {
 // fill in the form here
return browser.getAllWindowHandles().then(function (handles) {
    var newWindowHandle = handles[1]; // this is your new window
    return browser.switchTo().window(newWindowHandle).then(function () {
        return browser.sleep(5000).then(function () {
            return browser.actions().sendKeys(protractor.Key.ESCAPE).perform().then(function () {
                return browser.switchTo().window(handles[0])
            });
        });
    });
});

But It does not open a new tab for print Dialog. Opens print Dialog in same tab. I am not getting any response.

1

The problem is that you cannot control the Print dialog through Selenium.

One of the most straightforward solutions in my opinion is to disable the "print" functionality by overriding the window.print function dynamically. Translating this solution to JavaScript:

var printButton = $(".print");
expect(printButton.isPresent()).toBe(true);

browser.actions().mouseMove(printButton).perform();

// verify that button was clicked
browser.executeAsyncScript(function () {
   var callback = arguments[1];

   // disable print and "watch" for it to be called
   window.print = function() { callback(); };

   arguments[0].click();
}, printButton.getWebElement());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.