July 17, 2015

Keys Simulation using Selenium Webdriver

Here is the post how to simulate keys actions using selenium webdriver..Keys are used simulate user keyboard actions. For example if you want Press Enter, PageUp, Page Down, ALT or Tabs keys using selenium. It can be achieved by using Keys class..

For example after entering some text in search box you need not to click submit/search button. you can simulate ENTER key actions by using Keys class.

To achieve this we have to use Advanced User Interaction API.


More details about Advanced User Interactions can be found here --  Actions

In order to use AUI we need to create an object for Actions class.

In below scenario i will show how to Use Actions class and simulate Enter Key, Page Up and Page Down keys.


package sample;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
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 Keys_Test {
 
 public WebDriver driver;

 @Test
 public void testKeys_Test() throws Exception {
  driver.get("http://www.amazon.in/");
  WebElement txtSearch=driver.findElement(By.id("twotabsearchtextbox"));
  txtSearch.sendKeys("mobiles");
  //Create an object for Actions class
  Actions a = new Actions(driver);
  //Simulate Enter key
  a.sendKeys(Keys.ENTER).perform();
  //Simulate Page Down
  a.sendKeys(Keys.PAGE_DOWN).perform();
  //Simulate page Up
  a.sendKeys(Keys.PAGE_UP).perform();
  Thread.sleep(2000);
 }

 @BeforeClass
 public void beforeClass() 
 {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }

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

5 comments:

  1. Thanks for your informative post on selenium automation testing tool. Your article helped me a lot in understanding the future of automation testing tool and its career prospects. Selenium Training | Best Selenium training institute in Chennai|Software testing training

    ReplyDelete
  2. Great post about Keys Simulation using Selenium Webdriver!! Really its working good for me. Thanks for your effort!!
    Selenium training in Chennai | Best Selenium training institute in Chennai

    ReplyDelete
  3. Selenium allows the end users to share and write extensions or other code modifications, even the ones that are project specific. This gives ample freedom to selenium testing companies to write custom functions that do sophisticated manipulations. Using such custom functions allows tester to have more readable tests. Selenium Automation Training London

    ReplyDelete