Selenium WebDriver-Opening a link of a webpage in a new tab and new page
Opening a link of a webpage in a new tab can be done by Ctrl (Key) + Click
and opening a link in a new page by Shift (Key) + Click.
To perform this we need to import the package org.openqa.selenium.Keys.
As you can observe in the below example.
String selectAll = Keys.chord(Keys.SHIFT,Keys.RETURN);
String tab=Keys.chord(Keys.CONTROL,Keys.RETURN);
Wondering what is Keys and Chord ?
Keys is an enum inherited from java.lang.Enum<Keys>.
Chord is a method used to simulate pressing many keys at once.
CONTROL and SHIFT are Enum constants used to press Control key and Shift in the key board.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class Openinglinkintabpage {
WebDriver driver=new FirefoxDriver();
String defaultwindow = "";
@Test
public void pagetest() throws InterruptedException {
driver.get("http://www.google.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
defaultwindow = driver.getWindowHandle();
driver.findElement(By.name("q")).sendKeys("test");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("gbqfb")).click();
//1. Opens a link in a new page
String selectAll = Keys.chord(Keys.SHIFT,Keys.RETURN);
driver.findElement(By.linkText("Images")).sendKeys(selectAll);
//2.Open a link a new tab
String tab=Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("Images")).sendKeys(tab);
}
}
Comments
Post a Comment