Search code examples
javaseleniumassertwindow-handles

How do I assert true if the popup window exists?


Basically I want to be able to assert true if the popup window appears when I click a button which it currently does. How would I go about doing this? Here is my code:

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


driver.close();

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

I am switching to that window then closing it and going back to the original. But I would like to be able to assert true if the window pops up.


Solution

  • Here's what I usually do:

    int windowCount = driver.getWindowHandles().size();
    printPDF.click();
    assertEquals(windowCount + 1, driver.getWindowHandles().size());
    // or the Hamcrest way, if you're familiar with it
    assertThat(driver.getWindowHandles(), hasSize(windowCount + 1));
    // or the FEST way, if you're familiar with it
    assertThat(driver.getWindowHandles()).hasSize(windowCount + 1);