HANDLING MULTIPLE WINDOWS IN WEBDRIVER
Selenium WebDriver Switch Window Commands
Some web applications have many frames or multiple windows. Selenium WebDriver a
ssigns an alphanumeric id to each window as soon as the WebDriver object is inst
antiated. This unique alphanumeric id is called window handle. Selenium uses thi
s unique id to switch control among several windows. In simple terms, each uniqu
e window has a unique ID, so that Selenium can differentiate when it is switchin
g controls from one window to the other.
GetWindowHandle Command
Purpose: To get the window handle of the current window.
String handle= [Link]();//Return a string of alphanumeric wind
ow handle
String handle= [Link]();//Return a string of alphanumeric wind
ow handle
GetWindowHandles Command
Purpose: To get the window handle of all the current windows.
Set<String> handle= [Link]();//Return a set of window handle
Set<String> handle= [Link]();//Return a set of window handle
SwitchTo Window Command
Purpose: WebDriver supports moving between named windows using the
.
switchTo
method
[Link]().window("windowName");
[Link]().window("windowName");
Or
Alternatively, you can pass a window handle to the switchTo().window()
ng this, it s possible to iterate over every open window like so:
method. Knowi
for (String handle : [Link]()) {
[Link]().window(handle);}
for (String handle : [Link]()) {
[Link]().window(handle);}
Or
Switching between windows with Iterators:
[Link]([Link]( id of the link which opens new window )).click();
//wait till two windows are not opened
waitForNumberofWindowsToEqual(2);//this method is for wait
Set handles = [Link]();
firstWinHandle = [Link](); [Link](firstWinHandle);
String winHandle=[Link]().next();
if (winHandle!=firstWinHandle){
//To retrieve the handle of second window, extracting the handle which does not
match to first window handle
secondWinHandle=winHandle; //Storing handle of second window handle
//Switch control to new window
[Link]().window(secondWinHandle);
[Link]([Link]( id of the link which opens new window )).click();
//wait till two windows are not opened
waitForNumberofWindowsToEqual(2);//this method is for wait
Set handles = [Link]();
firstWinHandle = [Link](); [Link](firstWinHandle);
String winHandle=[Link]().next();
if (winHandle!=firstWinHandle){
//To retrieve the handle of second window, extracting the handle which does not
match to first window handle
secondWinHandle=winHandle; //Storing handle of second window handle
//Switch control to new window
[Link]().window(secondWinHandle);
SwitchTo Frame Command
Purpose: WebDriver supports moving between named frames using the
switchTo method.
[Link]().frame("frameName");
[Link]().frame("frameName");
SwitchTo PopUp Command
Purpose: WebDriver supports moving between named PopUps using the switchTo method.
After you ve triggered an action that opens a popup, you can access the alert and
it will return the currently open alert object. With this object you can now ac
cept, dismiss, read its contents or even type into a prompt. This interface work
s equally well on alerts, confirms, and prompts.
Alert alert = [Link]().alert();
Alert alert = [Link]().alert();
public class MultiWindowHandle {
WebDriver driver;
@Before
public void setup() throws Exception {
driver=new FirefoxDriver();
String URL="[Link]
[Link](URL);
[Link]().window().maximize();
}
@Test
public void test() throws Exception {
// Opening Calender
[Link]([Link]("//img[@alt='Calender']")).click();
// Storing parent window reference into a String Variable
String Parent_Window = [Link]();
// Switching from parent window to child window
for (String Child_Window : [Link]())
{
[Link]().window(Child_Window);
// Performing actions on child window
[Link]([Link]("calendar_month_txt")).click();
List Months=[Link]([Link]("//div[@id='monthDropDown']//div"));
int Months_Size=[Link]();
[Link]("Month size is:"+Months_Size);
[Link](1).click();
[Link]([Link]("//*[@id='calendarDiv']/div/table/tbody/tr/td[conta
ins(text(),'16')]")).click();
}
//Switching back to Parent Window
[Link]().window(Parent_Window);
//Performing some actions on Parent Window
[Link]([Link]("btn_style")).click();
}
@After
public void close() {
[Link]();
}
}
------------------------package practiceTestCases;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class PracticeSwitchWindow {
public static WebDriver driver;
public static void main(String[] args) {
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
// Put an Implicit wait, this means that any search for elements
on the page could take the time the implicit wait is set for before throwing ex
ception
[Link]().timeouts().implicitlyWait(10, [Link]);
// Launch the URL
[Link]("[Link]
ch-windows/");
// Store and Print the name of the First window on the console
String handle= [Link]();
[Link](handle);
// Click on the Button "New Message Window"
[Link]([Link]("New Message Window")).click();
// Store and Print the name of all the windows open
Set handles = [Link]();
[Link](handles);
// Pass a window handle to the other window
for (String handle1 : [Link]()) {
[Link](handle1);
[Link]().window(handle1);
}
// Closing Pop Up window
[Link]();
// Close Original window
[Link]();
}
}
----------------------package practiceTestCases;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class PracticeSwitchWindow {
public static WebDriver driver;
public static void main(String[] args) {
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
// Put an Implicit wait, this means that any search for elements
on the page could take the time the implicit wait is set for before throwing ex
ception
[Link]().timeouts().implicitlyWait(10, [Link]);
// Launch the URL
[Link]("[Link]
ch-windows/");
// Click on the Button "Alert Box"
[Link]([Link]("Alert Box")).click();
// Switch to JavaScript Alert window
Alert myAlert = [Link]().alert();
// Accept the Alert
[Link]();
// Close Original window
[Link]();
}
}
-----------------------------------