How to open a link in a new tab or window and switch to work with using Selenium

package SeleniumLearn.Com;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NewTab {
public WebDriver driver;
@Test  
      public void Newtab() throws Exception {
         driver.get("http://www.seleniumlearn.com"); 
         driver.findElement(By.linkText("Home")).sendKeys(Keys.CONTROL, "t");
         driver.navigate().to("http://in.linkedin.com/in/purushothamk"); 
         driver.findElement(By.linkText("Personal Website")).sendKeys(Keys.CONTROL, "n");
      Set<String> window1=driver.getWindowHandles();
      Object s[]=window1.toArray();
         driver.switchTo().window(s[1].toString());
         driver.get("https://twitter.com/seleniumlearn"); 
       }
 @BeforeTest
       public void beforeTest() {
          driver = new FirefoxDriver();
          driver.manage().window().maximize();
          }}

Tags: 

Comments