Perform operations on new window

There are cases where we need to open new window and perform operations or there may be cases where after clicking on any button / link, it opens new window and need to perform operations on the new window.

Let us look into such example:

Test case: We need to open '' and click on 'Help Center' link at the bottom which will open new window.
1. Verify the title of the new window
2. Verify text 'Welcome' on the page.
3. Search for a Question with text "Frequently Asked Questions" and verify the result.

package com.pack;

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class WindowExamples {
	public static WebDriver driver;

	public void verifySearchInNewWindow() throws InterruptedException {
		driver = new FirefoxDriver();
		String mainHandle = driver.getWindowHandle();
		//Wait for the element to be present
		WebDriverWait wait = new WebDriverWait(driver, 5);
		driver.findElement(By.linkText("Help Center")).click();
		//Switch to new window and verify the title
		String newTitle = getCurrentWindowTitle();
		Assert.assertEquals(newTitle, "LinkedIn Help Center", "New window title is not matching");
		//Verify the text present on the page
		String textOnpage=driver.findElement(By.cssSelector(".welcome")).getText().trim();
		Assert.assertEquals(textOnpage, "Welcome!");
		//Verify search text on the page
		String searchText="Frequently Asked Questions";
		WebElement searchInputBox=driver.findElement("kw"));
		WebElement searchButton = driver.findElement(By.cssSelector(""));;
		WebElement resultedElement = driver.findElement(By.cssSelector(".rn_Element2"));
		String resultedText = resultedElement.getText().trim();
		Assert.assertTrue(resultedText.contains(searchText), "Search successfull");
		closeAllOtherWindows(driver, mainHandle);

To execute the above test we have created a methods which can be reused with multiple tests.

Below method is used to get the main window handle. we will the driver as parameter.

	public static String getMainWindowHandle(WebDriver driver) {
		return driver.getWindowHandle();

Below method is used to get the current window title

	public static String getCurrentWindowTitle() {
		String windowTitle = driver.getTitle();
		return windowTitle;

Below method is used to close all the other windows except the main window.

	public static boolean closeAllOtherWindows(WebDriver driver, String openWindowHandle) {
		Set<String> allWindowHandles = driver.getWindowHandles();
		for (String currentWindowHandle : allWindowHandles) {
			if (!currentWindowHandle.equals(openWindowHandle)) {
		if (driver.getWindowHandles().size() == 1)
			return true;
			return false;

Below method is used to wait for the new window to be present and switch to it.

	 public static void waitForNewWindowAndSwitchToIt(WebDriver driver) throws InterruptedException {
	        String cHandle = driver.getWindowHandle();
	        String newWindowHandle = null;
	        Set<String> allWindowHandles = driver.getWindowHandles();
	        //Wait for 20 seconds for the new window and throw exception if not found
	        for (int i = 0; i < 20; i++) {
	            if (allWindowHandles.size() > 1) {
	                for (String allHandlers : allWindowHandles) {
	                    if (!allHandlers.equals(cHandle))
	                    	newWindowHandle = allHandlers;
	            } else {
	        if (cHandle == newWindowHandle) {
	            throw new RuntimeException(
	                    "Time out - No window found");

Note: When ever we work on multiple windows, switching plays major role. We should switch to the desired window to perform operations and again switch back to default window to work on main window.

Selenium Tutorials: 

Add new comment