Sam Kohnson
Sam Kohnson

Reputation: 115

Selenium check if a window is currently open

So I have a selenium webdriver logging me into a website and from there I click a button which opens a second window. I use this code below to switch to the new window

 String winParent = driver.getWindowHandle();

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

Now on that second window I run some automation . Once complete I would press the save button and this would close the current window. If the window doesn't close it means I have some errors on the page and I need to loop back and fix it.

 driver.findElement(By.id("btnSave")).click();

  if (isAlive(driver) == false) {
      //break and exit
      System.out.println("cic" + name);
      finalString = finalString + "cic: " + name;
      break;

   }


  public Boolean isAlive(WebDriver driver) {
    try {
        driver.getCurrentUrl();//or driver.getTitle();
        return true;
    } catch (Exception ex) {
        return false;
    }
}

The The program works as expected when it catches the errors and the window doesn't close. But as soon as everything is clear and the window closes it enters the if statement above and displays this error.

Unable to receive message from renderer.

I believe that I'm not checking if the window has been closed correctly.

edit: after some debugging it seems like once the window closes the program can't really tell what to do next. https://i.sstatic.net/JkeLG.png

Upvotes: 1

Views: 9398

Answers (3)

alboforlizo
alboforlizo

Reputation: 371

Assuming driver and window handle (wHandle) are class members:

private boolean isNewWindowOpened()
        {
        Set<String> s = this.driver.getWindowHandles();
        Iterator<String> ite = s.iterator();
        while( ite.hasNext() )
            {
            String popupHandle = ite.next().toString();
            if( popupHandle.equals( this.wHandle ) )
                {
                return true;
                }
            }
        return false;
        }

Upvotes: 0

Yash Patel
Yash Patel

Reputation: 161

I came across the same situation, I've got a solution for you, check window count after clicking on "Save" button. Ideally, there will be one window if you have provided all the correct data and if not then there are two windows.

    driver.findElement(By.id("btnSave")).click();
    
      if (driver.getWindowHandled().size() >= 2) {
          // make required changes
          // again click on save button
       } else {
          //break and exit
          System.out.println("cic" + name);
          finalString = finalString + "cic: " + name;
          break;
       }
    }

Upvotes: 0

Ivan Pronin
Ivan Pronin

Reputation: 1916

I suggest using windowHandle for this. You are saving initial window in String winParent = driver.getWindowHandle();

Then you switch to the second window, which will have different handle. When you need to check if the second window is still open, just use:

  private boolean isNewWindowOpened(WebDriver driver, String parentWindowHandle) {
    try {
        return !driver.getWindowHandle().equals(parentWindowHandle);
    } catch (Exception ex) {
        driver.switchTo(parentWindowHandle);
        return false;
    }

Upvotes: 2

Related Questions