Saturday, January 25, 2014

How to switch between different windows using Selenium WebDriver


Inorder to switch between Windows we should be knowing the window handlers and traverse between windows.

For that i am opening the link in a new window using clicking down button, after that moving to the specified window.

Here is the code:
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class MoveBetweenTabs
{
    public static void main(String[] args)
    {
        WebDriver driver=new FirefoxDriver();
        driver.navigate().to("http://www.google.com");
       
        driver.manage().window().maximize();
       
        WebElement oWE=driver.findElement(By.linkText("About Google"));
       
        Actions oAction=new Actions(driver);
        oAction.moveToElement(oWE);
        oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
       
        Set<String> sHandlers= driver.getWindowHandles();
        for(String sHandler:sHandlers)
        {
       
            if(driver.switchTo().window(sHandler).getTitle().equals("Google"))
            {
                driver.switchTo().window(sHandler);
                WebElement oWE1=driver.findElement(By.linkText("+Google"));
                oWE1.click();
            }
        }
    }
}

No comments:

Post a Comment