Skip to toolbar

Windows Handling | Multiple windows handling using JavascriptExecutor.

Traversing through multiple windows using ‘JavascriptExecutor’ and selecting user-defined tab window with ‘Title Partial Text’. This process is called multiple windows handling.

               Some times we have to open multiple windows (tabs) as per the requirement and need to switch the control through various tabs to select user defined tab window. For this, we can use partial text from the window title along with the switchTo statement.



Step to handle multiple windows:

  1. Launch the browser and open the main URL(http://www.allinoneblogs.com)
  2. Now set a keyword for the expected window using a String variable with partial window text on which user wants to navigate.
  3. Now launch five different tab windows with different URLs using JavascriptExecutor
  4. Use ‘Set collection‘ to get the list of all opened window at a time. (Note: A ‘Set collection’ contains no duplicate values in it.)
  5. Now traverse through each window using ‘foreach‘ loop and switchTo statement.
  6. compare the current window title with the expected window title.
  7. Print the complete Title of expected tab window.

Code Example:

import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingMultipleWindow {

	public static void main(String[] args) throws InterruptedException 
	{
		String Url_1="http://www.allinoneblogs.com";
		String Url_2="http://www.allinoneblogs.com/category/selenium-tutorials/headless-testing/";
		String Url_3="http://www.allinoneblogs.com/contact/";
		String Url_4="http://www.allinoneblogs.com/gallery/";
		String Url_5="http://www.allinoneblogs.com/overview/";
		
		// Initialze variable with partial text present in the title.
		// This would be case sensitive.
		String targetTabWindowTitle="Contact";
		
		System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		driver.manage().window().maximize();
		driver.get(Url_1);
		
		JavascriptExecutor jse= (JavascriptExecutor)driver;
		
		// Script to launch blank window.
		// jse.executeScript("window.open('about:blank','_blank');");
		
		// Script to launch more windows with different URLs
		jse.executeScript("window.open('"+Url_2+"','_blank');");
		jse.executeScript("window.open('"+Url_3+"','_blank');");
		jse.executeScript("window.open('"+Url_4+"','_blank');");
		jse.executeScript("window.open('"+Url_5+"','_blank');");
		
		// Getting list of all opened windows
		Set multipleWindows= driver.getWindowHandles();
		
		// Use 'foreach()' loop to traverse through all windows
		for (String currentWindow : multipleWindows) 
		{
			Thread.sleep(200);
			driver.switchTo().window(currentWindow);
			String currentWindowTitle=driver.getTitle();
			
		// Compare current window title with expected title.
			if(currentWindowTitle.contains(targetTabWindowTitle))
			{
				System.out.println("Current Window: "+currentWindowTitle);
				break;
			}	
		}
	}
}

3501total visits,5visits today

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 4
  • Page views today : 4
  • Total visitors : 48,244
  • Total page view: 66,391

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.