MainMenu

Home Core Java Overview Maven Tutorials

Monday, 14 May 2018

How To Perform Right Click Action (Context Click) In Selenium


Hello Friends,

In this article , we will learn , how to perform right click of mouse & select option "Open Link in New Tab" and "Open Link in New Window" using selenium webdriver.

Open link in new Tab :
So we will take an easy scenario as below :
Launch the browser
Navigate to the URL
right click on any link
select the option "Open Link in New Tab"
switch to new tab & perform some operation
close the new tab & switch back to parent tab.

To achieve this we will write a simple code in TestNG.

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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Opennewtab
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\cchauhan\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
}
@Test
public void h()
{
WebElement seleniumtutorial = driver.findElement(By.xpath(".//*[@id='post-body-751561208295527719']/center/div[1]/table/tbody/tr[1]/td[4]/a/img"));
Actions action = new Actions(driver);
action.contextClick(seleniumtutorial).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String windowhandler = driver.getWindowHandle();
ArrayList tabs2 = new ArrayList (driver.getWindowHandles());
System.out.println(tabs2.size());
driver.switchTo().window((String) tabs2.get(1));
String link = driver.findElement(By.xpath(".//*[@id='post-body-7737945062927568046']/table[1]/tbody/tr[1]/td[1]/h2/a")).getAttribute("href");
System.out.println(link);
driver.close();
driver.switchTo().window(windowhandler);
System.out.println("i am working fine");
}
}


Open link in new window :
Now again scenarios will be same but opeartion will be performed in new window instead of new tab :

Launch the browser
Navigate to the URL
right click on any link
select the option "Open Link in New window"
switch to new window & perform some operation.
close the new window & switch back to parent window.
To achieve this we will write a simple code in TestNG.

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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class OpennewWindow
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\cchauhan\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
}
@Test
public void h() throws InterruptedException
{
WebElement seleniumtutorial = driver.findElement(By.xpath(".//*[@id='post-body-751561208295527719']/center/div[1]/table/tbody/tr[1]/td[4]/a/img"));
Actions action = new Actions(driver);
action.contextClick(seleniumtutorial).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String windowhandler = driver.getWindowHandle();
ArrayList tabs2 = new ArrayList (driver.getWindowHandles());
System.out.println(tabs2.size());
driver.switchTo().window(tabs2.get(1));
String link = driver.findElement(By.xpath(".//*[@id='post-body-7737945062927568046']/table[1]/tbody/tr[1]/td[1]/h2/a")).getAttribute("href");
System.out.println(link);
Thread.sleep(3000);
driver.close();
driver.switchTo().window(windowhandler);
System.out.println("i am working fine");
}
}

No comments:

Post a Comment