Wednesday, April 27, 2016

Handling multiple windows in selenium 2.0 (webdriver)

Here I am trying to explain the concept of Handling Multiple windows in Selenium WebDriver. Selenium WebDriver has built in methods available to switch from one window to another window.
We will use :
WebDriver.getWindowHandles()
WebDriver.switchTo().window()

I am just pasting here the working code and that you can use in ur eclipse and try seeing how things works.

I am taking example of "www.timesjob.com" where you can click on links given in home page (top left side: ) to open multiple windows in webdriver.

I tried to explain the step by step action taken in code and hope this will help you understanding the code in better way.
---------------------------------------------------------------------------------------------------------------------------
import java.util.Set;

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

public class HandlingWindows
{

    public static void main(String[] args)
    {
          
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.timesjobs.com/");

/*below i m clicking to links given in home page which opening new windows*/

        driver.findElement(By.xpath("html/body/div[1]/div[3]/header/nav[1]/ul/li[2]/a")).click();
        driver.findElement(By.xpath("html/body/div[1]/div[3]/header/nav[1]/ul/li[3]/a")).click();
        driver.findElement(By.xpath("html/body/div[1]/div[3]/header/nav[1]/ul/li[4]/a")).click();

/*so till now webdriver has opened 4 windows, one parent and 3 child. Now here next statement printing the current url that is of parent window.*/

        System.out.println(driver.getCurrentUrl());

//Now using getWindowHandles method and get all handles of open window

        Set AllWindow= driver.getWindowHandles();
        String window1 = (String) AllWindow.toArray()[0];
        String window2=  (String) AllWindow.toArray()[1];
        String window3=  (String) AllWindow.toArray()[2];
        String window4=  (String) AllWindow.toArray()[3];


// Now here we can switch to any window using switchTo()

        driver.switchTo().window(window2);
        System.out.println(driver.getCurrentUrl());
/* I am just prinmting the URL to check if window is switching or not. here you can perform any operation in current focused window*/

        driver.switchTo().window(window3);
        System.out.println(driver.getCurrentUrl());

        driver.switchTo().window(window4);
        System.out.println(driver.getCurrentUrl());

        driver.switchTo().window(window1);
        System.out.println(driver.getCurrentUrl());
       
       driver.quit();
       
    }

}
---------------------------------------------------------------------------------------------------------------------
The output is :
http://www.timesjobs.com/
http://jobbuzz.timesjobs.com/
http://stepahead.timesjobs.com/
http://www.techgig.com/
http://www.timesjobs.com/

I hope this is simple way of learning the concept and now can try advance things.

No comments :

Post a Comment