package com.switchto.test;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WindowHandleDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.navigate().to("http://anish-selenium.blogspot.in/p/multiple-browser-window-handles_8.html");
Thread.sleep(3000);
System.out.println("Current Window Title is: " + driver.getTitle());
//Get the current Window that is open by WebDriver instance
String parentWindow = driver.getWindowHandle();
System.out.println("Current window Id is: " + parentWindow);
driver.findElement(By.linkText("Google")).click();
Thread.sleep(3000);
Set windows = driver.getWindowHandles();
String childWindow = null;
for (String window : windows) {
if(!window.equals(parentWindow)){
childWindow = window;
}
System.out.println(window);
}
driver.switchTo().window(childWindow);
System.out.println("Current Window Title is: " + driver.getTitle());
driver.findElement(By.name("q")).sendKeys("Selenium WebDriver");
driver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
driver.close();
driver.switchTo().window(parentWindow);
System.out.println("Current Window Title is: " + driver.getTitle());
driver.findElement(By.linkText("Facebook")).click();
Thread.sleep(5000);
windows = driver.getWindowHandles();
for (String window : windows) {
if(!window.equals(parentWindow)){
childWindow = window;
}
System.out.println(window);
}
driver.switchTo().window(childWindow);
System.out.println("Current Window Title is: " + driver.getTitle());
driver.findElement(By.id("email")).sendKeys("facebook@gmail.com");
driver.findElement(By.id("pass")).sendKeys("password");
driver.findElement(By.id("loginbutton")).click();
Thread.sleep(10000);
driver.close();
driver.switchTo().window(parentWindow);
driver.findElement(By.linkText("Twitter")).click();
Thread.sleep(4000);
windows = driver.getWindowHandles();
for (String window : windows) {
if(!window.equals(parentWindow)){
childWindow = window;
}
System.out.println(window);
}
driver.switchTo().window(childWindow);
System.out.println("Current Window Title is: " + driver.getTitle());
driver.quit();
}
}
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WindowHandleDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.navigate().to("http://anish-selenium.blogspot.in/p/multiple-browser-window-handles_8.html");
Thread.sleep(3000);
System.out.println("Current Window Title is: " + driver.getTitle());
//Get the current Window that is open by WebDriver instance
String parentWindow = driver.getWindowHandle();
System.out.println("Current window Id is: " + parentWindow);
driver.findElement(By.linkText("Google")).click();
Thread.sleep(3000);
Set
String childWindow = null;
for (String window : windows) {
if(!window.equals(parentWindow)){
childWindow = window;
}
System.out.println(window);
}
driver.switchTo().window(childWindow);
System.out.println("Current Window Title is: " + driver.getTitle());
driver.findElement(By.name("q")).sendKeys("Selenium WebDriver");
driver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
driver.close();
driver.switchTo().window(parentWindow);
System.out.println("Current Window Title is: " + driver.getTitle());
driver.findElement(By.linkText("Facebook")).click();
Thread.sleep(5000);
windows = driver.getWindowHandles();
for (String window : windows) {
if(!window.equals(parentWindow)){
childWindow = window;
}
System.out.println(window);
}
driver.switchTo().window(childWindow);
System.out.println("Current Window Title is: " + driver.getTitle());
driver.findElement(By.id("email")).sendKeys("facebook@gmail.com");
driver.findElement(By.id("pass")).sendKeys("password");
driver.findElement(By.id("loginbutton")).click();
Thread.sleep(10000);
driver.close();
driver.switchTo().window(parentWindow);
driver.findElement(By.linkText("Twitter")).click();
Thread.sleep(4000);
windows = driver.getWindowHandles();
for (String window : windows) {
if(!window.equals(parentWindow)){
childWindow = window;
}
System.out.println(window);
}
driver.switchTo().window(childWindow);
System.out.println("Current Window Title is: " + driver.getTitle());
driver.quit();
}
}
No comments:
Post a Comment