Sunday, 26 July 2015

Selenium WebDriver Switch Window Commands

GetWindowHandle Command
Purpose: To get the window handle of the current window.


 String  handle= driver.getWindowHandle();//Return a string of alphanumeric window handle


GetWindowHandles Command
Purpose: To get the window handle of all the current windows.


 Set<String> handle= driver.getWindowHandles();//Return a set of window handle


SwitchTo Window Command
Purpose: WebDriver supports moving between named windows using the “switchTo” method.


 driver.switchTo().window("windowName");

Or

Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:


 for (String handle : driver.getWindowHandles()) {

    driver.switchTo().window(handle);}
Or

Switching between windows with Iterators:


 driver.findElement(By.id(“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 = driver.getWindowHandles();

 firstWinHandle = driver.getWindowHandle(); handles.remove(firstWinHandle);

 String winHandle=handles.iterator().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

 driver.switchTo().window(secondWinHandle);

 driver.findElement(By.id(“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 = driver.getWindowHandles();

 firstWinHandle = driver.getWindowHandle(); handles.remove(firstWinHandle);

 String winHandle=handles.iterator().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

 driver.switchTo().window(secondWinHandle);


SwitchTo Frame Command
Purpose: WebDriver supports moving between named frames using the “switchTo” method.

driver.switchTo().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 accept, dismiss, read its contents or even type into a prompt. This interface works equally well on alerts, confirms, and prompts.


 Alert alert = driver.switchTo().alert();

No comments:

Post a Comment