Instantly share code, notes, and snippets.

Embed
What would you like to do?
package FrameAndWindow;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WindowDemo {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://qa.scrolltest.com");
try {
driver.manage().window().maximize();
driver.findElement(By.id("aut")).click();
String firstTab = driver.getWindowHandle();
System.out.println("First - "+firstTab);
Set<String> handles= driver.getWindowHandles();
handles.remove(firstTab);
String secondTab=handles.iterator().next();
if (secondTab!=firstTab){
driver.switchTo().window(secondTab);
System.out.println("secondTab - "+secondTab);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println(driver.getCurrentUrl());
driver.findElement(By.name("userName")).sendKeys("qahubtest");
driver.findElement(By.name("password")).sendKeys("test123");
driver.findElement(By.name("login")).click();
driver.findElement(By.linkText("SIGN-OFF")).click();
}else {
System.out.println("Error while Switching Tab");
}
}catch(Exception e) {
System.out.println(e.toString());
}
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment