package testngtest;
import java.util.Iterator;
import java.util.Set;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.testng.annotations.Test;
importorg.testng.annotations.BeforeMethod;
importorg.testng.annotations.AfterMethod;
public classTestNG_WindowHandleExample_by_jasmine {
// Declare global test varialbles
WebDriver jasminedriver = new FirefoxDriver();
String Open_a_popup_window;
String Positioned_Popup;
String JavaScript_Popup_Windows;
@Test
public void f() throws InterruptedException {
// A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.
Set windowids = jasminedriver.getWindowHandles();
//iterator( )
//Returns an Iterator object for the collection which may be used to retrieve an object
Iterator iter= windowids.iterator();
JavaScript_Popup_Windows=iter.next();
// Open first popup
jasminedriver.findElement(By.xpath("//*[@id='content']/div[3]/a")).click();
windowids = jasminedriver.getWindowHandles();
iter= windowids.iterator();
iter.next();
Open_a_popup_window=iter.next();
System.out.println(JavaScript_Popup_Windows);
System.out.println(Open_a_popup_window);
Thread.sleep(3000L);
System.out.println();
System.out.println("Positioned_Popup");
// switch to main window to click on other popup link
jasminedriver.switchTo().window(JavaScript_Popup_Windows);
// Open second popup
jasminedriver.findElement(By.xpath("//*[@id='content']/div[5]/p/a")).click();
windowids = jasminedriver.getWindowHandles();
iter= windowids.iterator();
iter.next();
iter.next();
Positioned_Popup = iter.next();
System.out.println(JavaScript_Popup_Windows);
System.out.println(Open_a_popup_window);
System.out.println(Positioned_Popup);
System.out.println();
Thread.sleep(3000L);
System.out.println("Centered_Popup");
}
@BeforeMethod
public void beforeMethod() {
jasminedriver.get("http://www.quackit.com/javascript/popup_windows.cfm");
}
@AfterMethod
public void afterMethod() {
jasminedriver.switchTo().window(Open_a_popup_window);
jasminedriver.close();
jasminedriver.switchTo().window(Positioned_Popup);
jasminedriver.close();
jasminedriver.switchTo().window(JavaScript_Popup_Windows);
jasminedriver.quit();
}