Handling non-modal popup windows

We will be using the website http://popuptest.com/goodpopups.html to demonstrate non-modal pop-up windows. We will write a small program to load this URL and then click the Good PopUp #1 link. A pop-up window will open and then we will fetch the window handles of both the open windows.

The following code clicks a link on the home page and fetches the window handles of both the open windows as well as their titles:

 WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://popuptest.com/goodpopups.html");
driver.findElement(By.xpath("//a[text()='Good PopUp #1']")).click();
Set openWindows = driver.getWindowHandles();
System.out.println("No of open windows: " + openWindows.size());
Iterator<String> it = openWindows.iterator();
String parent = it.next();
System.out.println("Parent Window: " + parent + " title: " + driver.getTitle());
String child = it.next();
System.out.println("Child Window:" + child + " title: "
+ driver.getTitle());
-------------------------------------------------------------------------------

Output:

No of open windows: 2
Parent Window: CDwindow-0F9E3C139C7C9BFEE7D9B981F83425D6 title: PopupTest - test your popup killer software
Child Window:CDwindow-1BD6347CF08C0721D66F726F9D04F52C title: PopupTest - test your popup killer software

Did you notice something funny about the output? We have two windows and two unique window handles but the title printed in both the windows is the same. How can that be possible? This happened because we did not switch windows before calling the getTitle() method. Here comes the concept of switching windows.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.83.150