Showing posts with label multiple windows. Show all posts
Showing posts with label multiple windows. Show all posts

Monday, 22 August 2016

Reusable(framework) methods for window handlers using selenium webdriver


There are many applications,where a link display on new window or tab when you click on it.
In that situation we have to switch driver focus from parent to child window and vice versa

In this post, I will explain how can we handle multiple windows using window Handlers in selenium webdriver.

Following reusable methods will help you for framework design.

         //Handling New window(tab) from Parent window(tab)
public static void handleNewTab(WebDriver driver)
{
Set<String> allWindowHandles = driver.getWindowHandles();
Iterator<String> iter=allWindowHandles.iterator();
int size=allWindowHandles.size();
String child = null;
for(int i=0;i<size;i++){
child=iter.next();
}

driver.switchTo().window(child);
}

//Handling Parent window(tab) from child window(tab)
public static void handleParentTab(WebDriver driver){

Set<String> allWindowHandles = driver.getWindowHandles();
String parent = (String) allWindowHandles.toArray()[0];
driver.switchTo().window(parent);

}

When user is working on more than two tabs(windows), following method will help if he wants to switch to second window(tab) from child tab(third window).

 //Handling child Parent window(tab)
 public static void handleChildParentTab(WebDriver driver){
Set<String> allWindowHandles = driver.getWindowHandles();
String window1 = (String) allWindowHandles.toArray()[1];
driver.switchTo().window(window1);

}


Sunday, 14 August 2016

Handle multiple windows using selenium webdriver in different ways

There are many applications,where a link display on new window or tab when you click on it.
In that situation we have to switch driver focus from parent to child window and vice versa

In this post, I will explain how can we handle multiple windows using windows Handlers in selenium webdriver.

We can handle multiple windows using window handlers in two ways.

1)Using For-Each Loop
2)Using For Loop

1.Using For-Each Loop:
Sample code:
public class HandleWindows {
public WebDriver driver;

@BeforeClass
public void setup() throws InterruptedException{
driver = new FirefoxDriver();
driver.get("http://www.way2selenium.com/");
Thread.sleep(5000);
driver.manage().window().maximize();
}
@Test
public void test(){

//To get the window id of the current window
String currentWindow = driver.getWindowHandle();

//Clicking on on share link on current window which will open in new window
driver.findElement(By.xpath("//span[contains(.,'Share to Facebook')]")).click();

//To get the window id's of all the current windows
Set<String> windows = driver.getWindowHandles();

//for each method for identifying child window
for(String ChildWindow:windows){

//switching from current window to child window
driver.switchTo().window(ChildWindow);

}

//Maximize child window
driver.manage().window().maximize();

//get the title from child window
System.out.println(driver.getTitle());

//Closing child window
driver.close();

//switch back from child to current window
driver.switchTo().window(currentWindow);

//get the title from current window
System.out.println(driver.getTitle());

//Closing current  window
driver.close();
}


2.Using For Loop:
Sample Code:
public class HandleWindows3 { public WebDriver driver; @BeforeClass public void setup() throws InterruptedException{ driver = new FirefoxDriver(); driver.get("http://www.way2selenium.com/"); Thread.sleep(5000); driver.manage().window().maximize(); } @Test public void test(){ //To get the window id of the current window String currentWindow = driver.getWindowHandle(); //Clicking on on share link on current window which will open in new window driver.findElement(By.xpath("//span[contains(.,'Share to Facebook')]")).click(); //For loop for identifying child window Set<String> allWindowHandles = driver.getWindowHandles(); Iterator<String> iter=allWindowHandles.iterator(); int size=allWindowHandles.size(); String child = null; for(int i=0;i<size;i++){ child=iter.next(); } //switching from current window to child window driver.switchTo().window(child); //Maximize child window driver.manage().window().maximize(); //get the title from child window System.out.println(driver.getTitle()); //Closing child window driver.close(); //switch back from child to current window driver.switchTo().window(currentWindow); //get the title from current window System.out.println(driver.getTitle()); //Closing current window driver.close(); } }