Sunteți pe pagina 1din 6

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= driver.getWindowHandle();//Return a string of alphanumeric wind
ow handle
String handle= driver.getWindowHandle();//Return a string of alphanumeric wind
ow 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
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");
driver.switchTo().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 : driver.getWindowHandles()) {


driver.switchTo().window(handle);}
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");
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 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 = driver.switchTo().alert();
Alert alert = driver.switchTo().alert();
public class MultiWindowHandle {
WebDriver driver;
@Before
public void setup() throws Exception {
driver=new FirefoxDriver();
String URL="https://www.abc.co.in/";
driver.get(URL);
driver.manage().window().maximize();
}
@Test
public void test() throws Exception {
// Opening Calender
driver.findElement(By.xpath("//img[@alt='Calender']")).click();
// Storing parent window reference into a String Variable
String Parent_Window = driver.getWindowHandle();
// Switching from parent window to child window
for (String Child_Window : driver.getWindowHandles())
{
driver.switchTo().window(Child_Window);
// Performing actions on child window
driver.findElement(By.id("calendar_month_txt")).click();
List Months=driver.findElements(By.xpath("//div[@id='monthDropDown']//div"));
int Months_Size=Months.size();
System.out.println("Month size is:"+Months_Size);
Months.get(1).click();
driver.findElement(By.xpath("//*[@id='calendarDiv']/div/table/tbody/tr/td[conta
ins(text(),'16')]")).click();
}
//Switching back to Parent Window
driver.switchTo().window(Parent_Window);
//Performing some actions on Parent Window
driver.findElement(By.className("btn_style")).click();
}
@After
public void close() {
driver.quit();
}
}
------------------------package practiceTestCases;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;
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
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch the URL
driver.get("http://toolsqa.wpengine.com/automation-practice-swit
ch-windows/");
// Store and Print the name of the First window on the console
String handle= driver.getWindowHandle();
System.out.println(handle);
// Click on the Button "New Message Window"
driver.findElement(By.name("New Message Window")).click();
// Store and Print the name of all the windows open
Set handles = driver.getWindowHandles();
System.out.println(handles);
// Pass a window handle to the other window
for (String handle1 : driver.getWindowHandles()) {
System.out.println(handle1);
driver.switchTo().window(handle1);
}
// Closing Pop Up window
driver.close();
// Close Original window
driver.quit();
}

}
----------------------package practiceTestCases;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
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
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch the URL
driver.get("http://toolsqa.wpengine.com/automation-practice-swit
ch-windows/");
// Click on the Button "Alert Box"
driver.findElement(By.name("Alert Box")).click();
// Switch to JavaScript Alert window
Alert myAlert = driver.switchTo().alert();
// Accept the Alert
myAlert.accept();
// Close Original window
driver.close();
}
}

-----------------------------------

S-ar putea să vă placă și