Friday, August 30, 2013

How To Open Tab And Switching Between Tabs In Selenium WebDriver



//To opens the new Tab
String ctrlT = Keys.chord(Keys.CONTROL, "t");
          driver.findElement(By.tagName("html")).sendKeys(ctrlT);
//To release the key
          action.keyUp(Keys.CONTROL);
//Come back to the 1st Tab
String ctrlTab = Keys.chord(Keys.CONTROL, "\t");
          driver.findElement(By.tagName("html")).sendKeys(ctrlTab);
//To release the key
          action.keyUp(Keys.CONTROL);

 

(OR)
 
public void openTab() {
  //Open tab 2 using CTRL + t keys.
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
  //Open URL In 2nd tab.
  driver.get("http://only-testing-blog.blogspot.in/2014/05/form.html");
 
  //Call switchToTab() function to switch to 1st tab
  switchToTab();
  //perform required actions on tab 1.
  driver.findElement(By.xpath("//input[@id='6']")).click();
   
  //Call switchToTab() function to switch to 2nd tab.
  switchToTab();
  //perform required actions on tab 2.
  driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys("hi");
  driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys("test");
 
  //Call switchToTab() function to switch to 1st tab
  switchToTab();
  //perform required actions on tab 1.
  String str = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Sum result Is -> "+str);
 }

 public void switchToTab() {
  //Switching between tabs using CTRL + tab keys.
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
  //Switch to current selected tab's content.
  driver.switchTo().defaultContent(); 
 }

No comments:

Post a Comment