Keyboard Actions in Selenium

package SeleniumLearn;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
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.AfterTest;

public class KeyBoardActions {
    WebDriver driver;
  @Test
  public void KeyBoard() throws Exception {
      driver.get("http://www.seleniumlearn.com");
      Actions act=new Actions(driver);
      act.contextClick(driver.findElement(By.id("page"))).perform();  // Right click action
      //Thread.sleep(2000);
      driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
     
      
      // OR

      WebElement we = driver.findElement(By.id("page"));
      for (int i=0; i<9; i++)
      {
      we.sendKeys(Keys.ARROW_DOWN);
      }   
      we.sendKeys(Keys.ENTER);
      
      //OR

      /*Robot rbt = new Robot();
      rbt.keyPress(KeyEvent.VK_ALT);
      rbt.keyPress(KeyEvent.VK_F4);
  */
      
    /*  WebElement test = driver.findElement(By.id("page"));
      for (int i=0; i<10; i++)
      {
          test.sendKeys(Keys.ARROW_DOWN);
      }   
      test.sendKeys(Keys.ENTER);*/
      
      //OR
      /*
      IntStream.iterate(1, n -> n + 1).limit(8).forEach(number ->
      driver.findElement(By.id("page")).sendKeys(Keys.ARROW_DOWN));
      driver.findElement(By.id("page")).sendKeys(Keys.ENTER);   */ 
    
  }
  @BeforeTest
  public void beforeTest() {
      driver=new FirefoxDriver();
      driver.manage().window().maximize();
  }

  @AfterTest
  public void afterTest() {
  }

}

Tags: 

Comments