0

Hello I want to learn how to switch in in to a new windows without using thread sleep. I was trying to use awaitility artifact but I was not able to done it correctly. I was trying to automate print window. When I click on print icons on my web page I navigate to print window I want to wait while navigating to print window and once print window displayed I want to click on cancel button. Can someone help me for that

Print_icon.click();
await().atMost(10,TimeUnit.SECOND).pollInterval(1,TimeUnit.SECONDS);
    Cancel_button.click();
  • Possible duplicate of [How can selenium web driver get to know when the new window has opened and then resume its execution](https://stackoverflow.com/questions/9188343/how-can-selenium-web-driver-get-to-know-when-the-new-window-has-opened-and-then) – NarendraR Sep 13 '18 at 06:04

2 Answers2

0

You can't handle windows dialogs so I suggest you review your page code and find the name of the method that opens the print window and override it. In your test case for example you can override print method to do nothing, like this:

((JavascriptExecutor)driver).executeScript("window.print=function(){};");
David Ryan
  • 15
  • 1
  • 3
Danial Jalalnouri
  • 609
  • 1
  • 7
  • 18
0

You can try this :

Print_icon.click();

for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle);
}

boolean elmnt = false;
boolean timeOut = false;
int second = 1;
do {
    try {
        if(second>30) {
            timeOut = true;
        }
        Cancel_button.click();
        elmnt=true;
    } catch (Exception e) {
        TimeUnit.SECONDS.sleep(1);
        second++;
    }
}while(elmnt==false && timeOut==false);
frianH
  • 7,295
  • 6
  • 20
  • 45