Switching between Windows using Selenium Webdriver

We come across to handle switching b/w windows while accessing different webpages. Inroder to automate switching b/w windows, following steps will help you to write Selenium Webdriver Script with the help of Java

It is not as easy as handling with Selenium Rc. We can achive with the help of getWindowHandle() and switchTo().window() methods.

Following is the Selenium Webdriver with Java script to handle popup windows:


package com.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SwitchWindow {

 public static void main(String[] args) throws Exception
 {
  FirefoxDriver driver = new FirefoxDriver();
  driver.get("http://www.quackit.com/html/codes/");
  // Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date
  String parentWindow = driver.getWindowHandle().toString();
  driver.findElement(By.linkText("Pop up windows")).click();
  Thread.sleep(3000);
  assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Example HTML Popup Window Code:[\\s\\S]*$"));
    driver.findElement(By.linkText("Open a popup window")).click();
    // to switch control to the new popup window by name
    driver.switchTo().window("popUpWindow");
    assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Copy/Paste HTML Codes[\\s\\S]*$"));
    // closing the popup window
  driver.close();
  // to switch control from popup window to another window, here parent window
  driver.switchTo().window(parentWindow);
  assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Example HTML Popup Window Code:[\\s\\S]*$"));
  driver.close();
 }
}

// Code for TestNG

package com.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
public class SwitchWindow {
 FirefoxDriver driver;
  @Test
  public void testSwitchWindow() {
   driver.get("http://www.quackit.com/html/codes/");
   // Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date
   String parentWindow = driver.getWindowHandle();
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*HTML Codes - Free[\\s\\S]*$"));
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Pop up windows[\\s\\S]*$"));
   driver.findElement(By.linkText("Pop up windows")).click();
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Open a popup window[\\s\\S]*$"));
   driver.findElement(By.linkText("Open a popup window")).click();
   // to switch control to the new popup window by name
   driver.switchTo().window("popUpWindow");
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Copy/Paste HTML Codes[\\s\\S]*$"));
   // closing the popup window
   driver.close();
   // to switch control from popup window to another window, here parent window
   driver.switchTo().window(parentWindow);
   assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Open a popup window[\\s\\S]*$"));
  }
  @BeforeClass
  public void beforeClass() {
   driver = new FirefoxDriver();
   driver.manage().window().maximize();
  }
  @AfterClass
  public void afterClass() {
   driver.close();
  }
}