Monday, 15 December 2014

Right Click using Actions class

Here's a basic program for performing right click using Actions class. The code will right click on a specified link, scroll to the second menu item and select it. In this case, the menu item is "Open Link In New Window" . However, this sample does not explain about switching windows, focus remains on right click option using webdriver.

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.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class ContextClickDemo {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

driver.navigate()
.to("http://www.toolsqa.com/automation-practice-form/");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Element on which to right click

WebElement we = driver.findElement(By
.xpath("//a[contains(@title,'Form')]"));

Actions act = new Actions(driver);

// Right click,and select second option from menu which will open a new window

act.contextClick(we).sendKeys(Keys.ARROW_DOWN)
.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
 //Instead of sendKeys(Keys.RETURN), we can also use sendKeys(Keys.ENTER)

driver.quit();

}

}


Check my next post "Toggling between windows"  for switching windows :)

Pay It Forward

DR

No comments:

Post a Comment