Using Robot We can Handle Multiple Tabs in Selenium
package samples;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
/**
*
* @author krishnareddy
*
*/
public class MultiTabs {
@Test
public void multiTabs() throws AWTException {
// System.setProperty("webdriver.chrome.driver",
// "Drivers\\chromedriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gtpvkr.in/2014/08/handling-authentication-dialog-box.html");
;
// driver.findElement(By.xpath(""));
//open new tab
new Actions(driver)
.contextClick(
driver.findElement(By
.xpath("//*[@id='rsidebar-wrapper']/div[1]/div/a[4]/img")))
.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build()
.perform();
// System.out.println(driver.getWindowHandles().size());
Robot robot = new Robot();
/*
* robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_TAB);
*
* robot.keyRelease(KeyEvent.VK_CONTROL);
* robot.keyRelease(KeyEvent.VK_TAB);
*/
//close current tab
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_W);
//get current tab title
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
//click on any element
driver.findElement(By.xpath("//*[@id='nav-legal']/li[1]/a")).click();
//get back previous tab
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_T);
}
}
package samples;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
/**
*
* @author krishnareddy
*
*/
public class MultiTabs {
@Test
public void multiTabs() throws AWTException {
// System.setProperty("webdriver.chrome.driver",
// "Drivers\\chromedriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gtpvkr.in/2014/08/handling-authentication-dialog-box.html");
;
// driver.findElement(By.xpath(""));
//open new tab
new Actions(driver)
.contextClick(
driver.findElement(By
.xpath("//*[@id='rsidebar-wrapper']/div[1]/div/a[4]/img")))
.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build()
.perform();
// System.out.println(driver.getWindowHandles().size());
Robot robot = new Robot();
/*
* robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_TAB);
*
* robot.keyRelease(KeyEvent.VK_CONTROL);
* robot.keyRelease(KeyEvent.VK_TAB);
*/
//close current tab
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_W);
//get current tab title
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
//click on any element
driver.findElement(By.xpath("//*[@id='nav-legal']/li[1]/a")).click();
//get back previous tab
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_T);
}
}