Identifying and handling a pop-up window by its content

In certain situations, developers neither assign a name attribute nor provide a title to the page displayed in a window. This becomes more complex when a test needs to deal with multiple windows open at the same time and identify the desired window.

As a workaround to this problem, we can check the contents of each window returned by the driver.getWindowHandles() method to identify the desired window.

How to do it...

Let's create a test that retrieves the handles of all the open windows in the current driver context. It will then iterate through this list, switching to the window and then checking for the content, which will help in identifying the correct window, as shown in the following code example:

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

  // Clicking Chat Button will open Chat Page in a new child window
  driver.findElement(By.id("chatbutton")).click();

  // There is no name or title provided for Chat Page window
  // We will iterate through all the open windows
  // and check the contents to find out if it's Chat window
  try {
    for (String windowId : driver.getWindowHandles()) {
      driver.switchTo().window(windowId);

      // We will use the page source to check the contents
      String pageSource = driver.getPageSource();

      if (pageSource.contains("Configuration - Online Chat")) {

        // Check the page for an element displaying a expected
        // message
        assertTrue(driver.findElement(By.tagName("p")).getText()
            .equals("Wait while we connect you to Chat..."));

        // Find the Close Button on Chat Window and close the window
        // by clicking Close Button
        driver.findElement(By.id("closebutton")).click();
        break;
      }
    }
  } finally {
    // Switch back to the parent browser window
    driver.switchTo().window(currentWindowId);
  }
  // Check driver context is in parent browser window
  assertEquals("Build my Car - Configuration", driver.getTitle());
}

How it works...

By calling the driver.getWindowHandles() method, the test will iterate through each open window, switching to the window and then checking if the desired content is present in the window with the help of the following code snippet:

for (String windowId : driver.getWindowHandles()) {
      driver.switchTo().window(windowId);

      // We will use the page source to check the contents
      String pageSource = driver.getPageSource();

      if (pageSource.contains("Configuration - Online Chat")) {

        // Check the page for an element displaying a expected
        // message
        assertTrue(driver.findElement(By.tagName("p")).getText()
            .equals("Wait while we connect you to Chat..."));

        // Find the Close Button on Chat Window and close the window
        // by clicking Close Button
        driver.findElement(By.id("closebutton")).click();
        break;
      }
    }

In this example, it checks for specific text appearing on a page by calling the driver.getPageSource() method.

If a window is found with the specific text, it will be closed by clicking on the Close button instead of calling the driver.close() method. You can implement this in your test when windows cannot be identified by using the name attribute or the title. This will help in building more reliable tests.

See also

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

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