Friday 21 February 2014

How to open the context menu in selenium webdriver in Java?

We can perform all kinds of mouse and keyboard events in selenium webdriver using Actions interface.

Please note that we need to import below classes to perform below operations.
import org.openqa.selenium.interactions.Action;

import org.openqa.selenium.interactions.Actions;

Java Example to right click (opening Context menu) on Webelement in Selenium Webdriver

package temp;
import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;



@SuppressWarnings("unused")
public class OpenGoogle {
   
public static void main(String [] arg)
{

System.setProperty("webdriver.chrome.driver", "C:\\SelenuimProject\\chromedriver2.8.exe");
WebDriver driver =  new ChromeDriver();

try{
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

driver.get("http://www.google.com/");

Thread.sleep(3000);

WebElement element = driver.findElement(By.id("hplogo"));

Actions builder = new Actions(driver);

//build the action chain.
Action rightclick = builder.contextClick(element).build();

//perform the action
rightclick.perform();


Thread.sleep(8000);


}

catch(Exception e){
       System.out.println("Exception - > " + e.toString());
       }
       finally{
              driver.close();
              driver.quit();
       }
}      //main function ends
      
}//class ends


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors