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(); } }




No comments:

Post a Comment