1

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. Thanks in advance.

EDIT

I have tried like this..

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.

NEXT EDIT-- I have 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 hungs up on print dialog as previous.

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

EDIT 3

I am having this problem in angular js not in java.

EDIT 4 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..open print Dialog in same tab.

Vikas Gupta
  • 1,183
  • 1
  • 10
  • 25
  • If this pdf open on another window, you just need to switch that window and close as `browser.switchTo().window(browser.getAllWindowHandles()[1]).close();...` – Saurabh Gaur Aug 29 '17 at 13:01
  • You can check edited post.[Saurabh](https://stackoverflow.com/users/3193455/saurabh-gaur) Please help. – Vikas Gupta Aug 29 '17 at 13:02
  • Please give a complete answer.. I am not getting..[Saurabh](https://stackoverflow.com/users/3193455/saurabh-gaur) – Vikas Gupta Aug 29 '17 at 13:04
  • [Have a look here](https://stackoverflow.com/questions/28788106/switching-to-new-window-with-selenium-protractor-javascript) to know how to switch window – Saurabh Gaur Aug 29 '17 at 13:04
  • Is there one window or multiple?? – Saurabh Gaur Aug 29 '17 at 13:21
  • Only one window. I want to open print dialog in next new tab. and then close it. And then go through next test cases in old windoows [Saurabh](https://stackoverflow.com/users/3193455/saurabh-gaur) – Vikas Gupta Aug 29 '17 at 13:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153140/discussion-between-vikash-777-and-saurabh-gaur). – Vikas Gupta Aug 29 '17 at 13:29
  • The print dialog is a browser specific dialog and cannot be interacted with with Selenium. – JeffC Aug 29 '17 at 18:43
  • Possible duplicate of [How to handle print dialog in Selenium?](https://stackoverflow.com/questions/11537103/how-to-handle-print-dialog-in-selenium) – JeffC Aug 29 '17 at 18:43
  • I am testing angular js application..not a Java application. This is a duplicate question but I have not got answer from that. Those answer have not solved my problem. [JeffC](https://stackoverflow.com/users/2386774/jeffc) So please remove that duplicate flag from my question. – Vikas Gupta Aug 30 '17 at 03:43

2 Answers2

0

You need to send the escape to the right window. Also wait for the window to be open before you send it. You probably also need to switch back to handles[0].

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])
            });
        });
    });
});
Barteld
  • 61
  • 7
  • Thanks for giving a try. [Barteld](https://stackoverflow.com/users/7306638/barteld) I have tried like above. It does not able to open second window for print dialog.looks Like browser.sleep is not working here – Vikas Gupta Aug 30 '17 at 04:41
  • does your function open a new tab, or only a print dialog? you only need to switch windows if your print option opens the dialog from a new tab. Otherwise you only need to wait for the dialog to be open. – Barteld Aug 31 '17 at 09:50
  • No, It does not open new tab.It only open a print Dialog [Barteld](https://stackoverflow.com/users/7306638/barteld) After clicking the print button, print dialog open so fast that like No browser.sleep works and therefore printDialog open in same windows. – Vikas Gupta Aug 31 '17 at 10:12
  • I'm not sure what you mean with 'print dialog open so fast that like No browser.sleep works'. browser.sleep is to delay the sending of the escape key. not to delay to dialog. The protractor browser class has no control over the dialog. – Barteld Aug 31 '17 at 12:07
  • I mean, I am tried like above, but no new tab is created for the print Dialog process. [Barteld](https://stackoverflow.com/users/7306638/barteld). This code also opens the print Dialog in same tab. – Vikas Gupta Aug 31 '17 at 12:17
0

Set the capabilities as below; this will disable the print preview pop up from chrome browser

capabilities: { 'browserName': 'chrome', chromeOptions: { args: ['--disable-print-preview','start-maximized'] }

},

Vithya
  • 1