Switch on new tab in same browser using Selenium Webdriver

package webdriverTestNG;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class NewTab {
    WebDriver driver;
    @Test
    public void Newtab() throws Exception {
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://seleniumlearn.com/selenium-tutorial");
    {

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(tabs.get(1));
    driver.get("http://www.fb.com/seleniumtool");
    driver.switchTo().window(tabs.get(0));
    
     //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();  
     // driver.switchTo().window(tabs.get(0));

driver.findElement(By.name("search_block_form")).sendKeys("selenium");
    }
    }              
    @BeforeTest
    public void beforeTest() {
        driver=new FirefoxDriver();
        //System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //--->chrome browser path
        //driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();  
    }

}

Comments