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

2078total visits,3visits today

Leave a Reply

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