Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell Selenium to press cancel on a print popup?

I am checking whether or not a page appears using Selenium. When I click the page, however, a printer print prompt appears (like the window that says select printer and such). How can I have Selenium close this window by hitting cancel?

I tried looking to alerts, but it seems like those will not work since the print window is a system prompt. It does not recognize any alerts appearing.

The most recent I tried using is by just sending keys like tab and enter in order to have the cancel button selected, however, it doesn't recognize any keys as being pressed.

How can I handle this case?

public static boolean printButton() throws Exception {

    WebDriver driver = new FirefoxDriver();
    driver.get("website");


    try {

        Thread.sleep(3000);
        WebElement temp = driver.findElement(By.xpath("//*[@id='block-print-ui-print-links']/div/span/a"));
        temp.click();
        Actions action = new Actions(driver); 
        action.sendKeys(Keys.TAB).sendKeys(Keys.ENTER);

        Thread.sleep(6000);

     }

     catch (Exception e) {

        System.out.println("No button.");
        driver.close();
        return false;

     }  
like image 548
J. Doe Avatar asked Apr 14 '16 18:04

J. Doe


3 Answers

I would simply disable the print dialog by overriding the print method :

((JavascriptExecutor)driver).executeScript("window.print=function(){};");

But if you goal is to test that the printing is called then :

// get the print button
WebElement print_button = driver.findElement(By.cssSelector("..."));

// click on the print button and wait for print to be called
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
((JavascriptExecutor)driver).executeAsyncScript(
    "var callback = arguments[1];" +
    "window.print = function(){callback();};" +
    "arguments[0].click();"
    , print_button);
like image 196
Florent B. Avatar answered Sep 21 '22 22:09

Florent B.


If you are going for testing only Chrome browser here is mine solution. Because of 'Robot' class or disabling print didn't work for my case.

// Choosing the second window which is the print dialog.
// Switching to opened window of print dialog.
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());

// Runs javascript code for cancelling print operation.
// This code only executes for Chrome browsers.
JavascriptExecutor executor = (JavascriptExecutor) driver.getWebDriver();
executor.executeScript("document.getElementsByClassName('cancel')[0].click();");

// Switches to main window after print dialog operation.
driver.switchTo().window(driver.getWindowHandles().toArray()[0].toString());

Edit: In Chrome 71 this doesn't seem to work anymore since the script can't find the Cancel button. I could make it work by changing the line to:

executor.executeScript("document.querySelector(\"print-preview-app\").shadowRoot.querySelector(\"print-preview-header\").shadowRoot.querySelector(\"paper-button.cancel-button\").click();");
like image 44
Erçin Akçay Avatar answered Sep 20 '22 22:09

Erçin Akçay


Actually you can't handle windows (OS) dialogs inside Selenium WebDriver. This what the selenium team answers here

The current team position is that the print dialog is out of scope for the project. WebDriver/Selenium is focused on emulating a user's interaction with the rendered content of a web page. Other aspects of the browser including, but not limited to print dialogs, save dialogs, and browser chrome, are all out of scope.

You can try different approach like AutoIt

like image 32
Eugene Avatar answered Sep 22 '22 22:09

Eugene