Friday 8 May 2015

Selenium:How to perform right click operations



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;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class RightClick {

   
    WebDriver driver;
      @Test
      public void rightclickoperation() throws InterruptedException {
          driver.get("http://www.bing.com/");
          driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
          //search
         driver.findElement(By.id("sb_form_q")).sendKeys("webdriver");
        // Thread.sleep(5000);
         driver.findElement(By.name("go")).click();
       
         WebElement clicker=driver.findElement(By.xpath(".//*[@id='b_results']/li[1]/h2/a"));
         Actions a=new Actions(driver);
       
         a.contextClick(clicker).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
       
         Thread.sleep(5000);
      }
      @BeforeClass
      public void beforeClass() {
          System.setProperty("webdriver.firefox.bin",
                    "C:\\Users\\IN00914\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
          driver=new FirefoxDriver();
          driver.manage().window().maximize();
          driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
         
      }

      @AfterClass
      public void afterClass() {
          driver.quit();
      }
   
}

No comments:

Post a Comment