Saturday 20 August 2016

Selenium: WebDriver: Switching among windows

Most of the time when you are writing test automation scripts, you need to deal with switching among windows. Selenium2 provides a convenient and easy way to handle this switching between windows.

Window Handle
Whenever you open a web page using WebDriver, WebDriver assigns a window handle to that. We use this window handle to switch across number of windows. WebDriver interface provides following methods to access and work with these window handles.

Method
Description
String getWindowHandle()
Return the current window handle.
Set<String> getWindowHandles()
Return set of window handles which can be used to iterate over all open windows.

Switching to windows using handles
WebDriver interface provides ‘switchTo’ method to switch across windows. Following statement switch to newWindowHandle.

driver.switchTo().window(newWindowHandle);


index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Selenium Tutorial</title>
</head>

<body>
  <h1>
    <a href="https://mail.google.com" target="_blank">gmail</a>
  </h1>
  <h1>
    <a href="https://www.google.co.in" target="_blank">google</a>
  </h1>
  <h1>
    <a href="https://www.facebook.com/" target="_blank">facebook</a>
  </h1>
  <h1>
    <a href="https://self-learning-java-tutorial.blogspot.com/"
      target="_blank">Java Tutorial</a>
  </h1>
</body>
</html>

Assume index.jsp is available at http://localhost:8080/application/.


App.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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;

public class App {
    private static List<String> handles = new ArrayList<>();

    private static void updateHandles(WebDriver driver) {
        Set<String> windowHandles = driver.getWindowHandles();

        for (String handle : windowHandles) {
            if (!handles.contains(handle)) {
                handles.add(handle);
            }
        }
    }

    public static void main(String[] args) throws IOException,
            InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://localhost:8080/application/");

        WebElement gmail = driver.findElement(By.linkText("gmail"));
        WebElement javaTutorial = driver.findElement(By
                .partialLinkText("Tutorial"));

        String mainWindow = driver.getWindowHandle();
        updateHandles(driver);
        System.out.println("mainWindow handle " + mainWindow);
        System.out.println("Total handles " + driver.getWindowHandles().size());

        gmail.click();
        updateHandles(driver);
        String gmailWindow = handles.get(handles.size() - 1);
        System.out.println("gmailWindow handle " + gmailWindow);
        System.out.println("Total handles " + driver.getWindowHandles().size());

        javaTutorial.click();
        updateHandles(driver);
        String javaTutorialWindow = handles.get(handles.size() - 1);
        System.out.println("javaTutorialWindow handle " + javaTutorialWindow);
        System.out.println("Total handles " + driver.getWindowHandles().size());

        System.out.println("Switching to main window");
        driver.switchTo().window(mainWindow);
        Thread.sleep(5000);

        System.out.println("Switching to gmail");
        driver.switchTo().window(gmailWindow);
        Thread.sleep(5000);

        System.out.println("Switching to Java Tutorial");
        driver.switchTo().window(javaTutorialWindow);
        Thread.sleep(5000);

        driver.quit();
    }
}


Output
mainWindow handle {3515d96d-b157-aa46-a6f8-b2baab92d158}
Total handles 1
gmailWindow handle {7001a995-0f7d-b04d-9d3e-ff8b45e7269a}
Total handles 2
javaTutorialWindow handle {2bf8b53d-01ce-db4e-aab2-c1168738c34a}
Total handles 3
Switching to main window
Switching to gmail
Switching to Java Tutorial





Previous                                                 Next                                                 Home

No comments:

Post a Comment