Identifying and handling a child window

In Selenium WebDriver, testing multiple windows involves identifying a window, switching the driver context to the window, then executing steps on the window, and finally, switching back to the browser.

The Selenium WebDriver allows us to identify a window by its name attribute or window handle, and switching between the window and the browser window is done using the WebDriver.switchTo().window() method of WebDriver.TargetLocator.

In this recipe, we will identify and handle a window by using its name attribute. Developers provide the name attribute for a window that is different from its title. In the following example, a user can open a window by clicking on the Help button. In this case, the developer has provided HelpWindow as its name:

<button id="helpbutton" onClick='window.open("help.html","HelpWindow","width=500,height=500");'>Help</button>

How to do it...

Let's create a test that identifies a window using its name attribute, as shown in the following code example:

package com.secookbook.examples.chapter06;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;

import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class WindowTest {

  public static WebDriver driver;

  @BeforeClass
  public static void setUp() {
    driver = new FirefoxDriver();
    driver.get("http://cookbook.seleniumacademy.com/Config.html");
    driver.manage().window().maximize();
  }

  @Test
  public void testWindowUsingName() {
    // Store WindowHandle of parent browser window
    String parentWindowId = driver.getWindowHandle();

    // Clicking Help button will open Help Page in a new child window
    driver.findElement(By.id("helpbutton")).click();

    try {
      // Switch to the Help window using name
      driver.switchTo().window("HelpWindow");

      try {
        // Check the driver context is in Help window
        assertEquals("Help", driver.getTitle());
      } finally {
        // Close the Help window
        driver.close();
      }
    } finally {
      // Switch to the parent browser window
      driver.switchTo().window(parentWindowId);
    }
    // Check driver context is in parent browser window
    assertEquals("Build my Car - Configuration", driver.getTitle());
  }

  @AfterClass
  public static void tearDown() {
    driver.quit();
  }
}

How it works...

The Selenium WebDriver provides a way to switch between the browser and windows and change the context of the driver. To move to a child window from the parent or the browser window, the driver.switchTo().window() method is used. This method accepts the name or handle attribute of the window. In the following example, the name attribute is used:

//Switch to the Help window
driver.switchTo().window("HelpWindow");

Now we can perform actions or verifications on the window through the driver instance as usual.

During a test, when you want to move to a window called from a parent window, save the parent window's handle attribute in a variable so that, when operations on the pop-up window are over and we want to switch back to the parent window, we can use the handle attribute, as follows:

//Save the WindowHandle of parent browser window
String parentWindowId = driver.getWindowHandle();

A test can switch back to the parent window using its handle attribute, as follows:

//Move back to the parent browser Window
driver.switchTo().window(parentWindowId);

Note

NoSuchWindowException: The driver.switchTo().window() method throws the NoSuchWindowException exception when it fails to identify the desired window.

There's more...

Windows can be closed by calling the driver.close() method. However, developers might implement the closing of a window by clicking on a button or a link. In this case, closing a window directly might lead to errors or exceptions.

See also

  • The Identifying and handling a window by its title recipe
  • The Identifying and handling a window by its content recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.90.182