Friday, 8 May 2015

Selenium:How to handle multiple windows using selenium web driver

package New;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class Multi_Windows {
    WebDriver driver;
    String winid;
  @Test
  public void multiplewindows() throws InterruptedException {
      driver.get("http://www.bing.com/");
      //driver.findElement(By.linkText("Outlook.com")).click();
      //driver.findElement(By.xpath(".//*[@id='outlook']")).click();
      //driver.findElement(By.xpath(".//*[@id='sc_hdu']/li[10]/a")).click();
     
      //office online
      driver.findElement(By.xpath("//*[@id='sc_hdu']/li[11]/a")).click();
     
      Thread.sleep(5000);
      //driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
      Set<String> element=driver.getWindowHandles();
      Iterator<String> itr=element.iterator();
      while(itr.hasNext())
      {
          winid=itr.next();
          System.out.println(winid);
      }
      driver.switchTo().window(winid);
      Thread.sleep(6000);
      driver.findElement(By.linkText("Products")).click();
      /*//driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
      driver.findElement(By.id("idDiv_PWD_UsernameExample")).sendKeys("hello@gmail.com");
      //driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
      driver.findElement(By.id("idDiv_PWD_PasswordExample")).sendKeys("hello");
    //  driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
      driver.findElement(By.id("idSIButton9")).click();*/
      Thread.sleep(6000);
     
  }
  @BeforeClass
  public void beforeClass() {
      driver=new FirefoxDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
     
  }

  @AfterClass
  public void afterClass() {
      driver.quit();
  }

}

No comments:

Post a Comment