Monday 25 February 2013

Switching between Windows using Selenium Webdriver

Purpose: 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();
  }
}

Switching between Windows using Selenium RC

Purpose: Script to swithc b/w windows using Selenium RC

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 RC Script with the help of Java

The major command that will help to switch b/w windows is selectWindow(). Using thisSelenium Rc command we achive the desired automation script.

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

// Code for JUnit

package com.seleniurc;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import static org.junit.Assert.*;

public class MultiWindows {
 private Selenium selenium;
 private SeleniumServer seleniumserver;
 private RemoteControlConfiguration rc;

 @Before
 public void setUp() throws Exception {
  rc = new RemoteControlConfiguration();
  seleniumserver = new SeleniumServer(rc);
  selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://");
  seleniumserver.start();
  selenium.start();
 }

 @Test
 public void testMultiWindows() throws Exception {
  //To open targeted webpage
  selenium.open("
http://www.quackit.com/html/codes/");
  // To hold the executing script for 5000 ms
  Thread.sleep(5000);
  selenium.click("link=Pop up windows");
  selenium.waitForPageToLoad("30000");
  // Perform click operation to open popup window
  selenium.click("link=Open a popup window");
  // waiting for popup window
  selenium.waitForPopUp("popUpWindow", "30000");
  // To switch b/w main window to popup window
  selenium.selectWindow("name=popUpWindow");
  Thread.sleep(3000);
  // executing text present command on popup window
  assertTrue(selenium.isTextPresent("Copy/Paste HTML Codes"));
  // closing popup window
  selenium.close();
  // Switching control to main window
  selenium.selectWindow(null);
  // executing text present command on popup window
  assertTrue(selenium.isTextPresent("Open a popup window"));
 }

 @After
 public void tearDown() throws Exception {
  selenium.stop();
  seleniumserver.stop();
 }
}


// Code for TestNG
package com.seleniurc;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

import com.thoughtworks.selenium.DefaultSelenium;
public class SwitchWindows {
 private DefaultSelenium selenium;
 private SeleniumServer server;
 private RemoteControlConfiguration rcc;
   @Test
   public void testSwitchWindows() throws InterruptedException {
    selenium.open("
http://www.quackit.com/html/codes/");
    assert(selenium.isTextPresent("HTML Codes - Free"));
    selenium.click("link=Pop up windows");
    //selenium.waitForPageToLoad("30000");
    assert(selenium.isTextPresent("HTML Popup Window Code"));
    Thread.sleep(3000);
    selenium.click("link=Open a popup window");
    selenium.waitForPopUp("popUpWindow", "30000");
    selenium.selectWindow("name=popUpWindow");
    assert(selenium.isTextPresent("Copy/Paste HTML Codes"));
    selenium.close();
    selenium.selectWindow(null);
    assert(selenium.isTextPresent("HTML Popup Window Code"));
    Thread.sleep(3000);
   }
   @BeforeClass
   public void beforeClass() throws Exception {
    rcc = new RemoteControlConfiguration();
    server = new SeleniumServer(rcc);
    selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
    server.start();
    selenium.start();
    selenium.windowMaximize();
   }

   @AfterClass
   public void afterClass() {
    selenium.stop();
    server.stop();
   }

}