Work with multiple browser windows

In the previous post,we have seen how to enter data into edit boxes and click on button. In this post ,we will see how to work with multiple browser windows.

This situation may come in any of your application, that when you click on any link or button , it will open a new browser window where you need to perform some desired tasks. We will see how to handle those situation.

Let’s take example from our demo application. We have 3 links over there , and if we click at the link ‘Learn Selenium!!’, it will open into a new window. It opens a Selenium Tutorials page. We will verify the title of the page to make sure that it has switched to the child browser. After that we will close this child browser and switch to the parent browser.

To verify that it has again switched to the parent browser, we will perform some activity on the parent browser e.g. let’s enter name in first name.

Link click

public class MultipleBrowser {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("http://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		WebElement link = driver.findElement(By.xpath("html/body/p[2]/a[1]"));
		//click on the link
		link.click();
		// get all window handles 
		Set<String> allWindow = driver.getWindowHandles();
		
		Iterator<String> iter = allWindow.iterator(); // create an iterator with String
		String Parent = iter.next();
		String child = iter.next();
		
		// Switch to child browser
		driver.switchTo().window(child);
		System.out.println("Title : "+driver.getTitle());
		driver.close();
		
		driver.switchTo().window(Parent);
		driver.findElement(By.xpath("//*[@id='firstname']")).sendKeys("Shekhar");
 	}
}
Output: Title : Selenium Tutorials | Testingpool

It has switched to the child browser which is Selenium Tutorials page as we can see it prints the title of the page successfully. It has switched to the parent window successfully, enter name in the edit box ‘First Name’.

Enter Name

Explanation of Function:

  • Open application and create a webelement named ‘link’ (Selenium learning!!)
  • Click on the link , it will open another browser window
  • getWindowHandles() :  This method returns browser window handles. Each browser handle is unique , with which selenium identifies the desired browser. It returns a set of Strings.
  • Iterator(): Iterate over the collection of window handles and provide parent window handle and child window handle.
  • switchTo(): This method helps to switch to parent to child window and vice versa.
  • driver.getTitle(): It will return the title of the current focused browser window.
  • driver.close(): Closing child browser.

 

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *