Learn Selenium and Java for Selenium

Saturday, 23 April 2016

Handling Keyboard Events in Webdriver

What are keyboard events?

Keyboard events are actions that can be performed with keyboard such as Alt F4, Shift "A", Control+Alt+Delete.There may be scenarios were we need to perform typing like a keyboard in certain fields

How Selenium handles keyboard events?

Selenium offers Action class in order to handle the keyboard events.The Action is a user facing API for emulating complex user action events.

There are different methods that are offered by Action Class. Most commonly used for keyboard events are
  • keyDown()- Press down a particular Key in keyboard, It can be Caps Lock or Shift button or any key present on keyboard
  • KeyUp()- UnPress the a particular key in keyboard.  It can be Caps Lock or Shift button or any key present on keyboard 
Here is the code snippets for using the Actions

//Configure the Action
Action builder=new Action(driver)

//To press a particular key(e.g Shift key)
builder.keydown(keys.SHIFT)


The below types a capital letter text on Email id field present on the facebook page


 import java.io.File;  
 import java.io.IOException;  
 import java.util.concurrent.TimeUnit;  
 import org.apache.commons.io.FileUtils;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.JavascriptExecutor;  
 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;  
 import org.testng.annotations.Test;  
 import org.*;  
  public class KeyboardEvents  
 {  
      public static void main(String[] args) {  
           WebDriver a = new FirefoxDriver();  
           a.manage().window().maximize();  
           a.get("http://www.facebook.com");  
           a.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);  
           Actions builder = new Actions(a);  
           WebElement e=a.findElement(By.id("email"));  
           Action writeCapital=builder.keyDown(Keys.SHIFT).sendKeys(e, "abc").keyUp(Keys.SHIFT).build();  
           writeCapital.perform();  
      }  
 }  


Notes

1-WebElement is a class in Selenium which is currently used to store the email field location
2-Action class offers keyDown method which is used to press down a particular key
3-.build() method is used to combine different keys method in a single command.For example in below case we are pressing some key then sending some value to the field and then unpress the same key, so 3 different types of action took place in order to combine them build() method is used
4-Perform method is used here to execute the action.

Output


Capital "ABC" is typed on email id field

Related Posts:

  • Strings in Java : Part 5 String Methods Continuing with commonly used method in the stings we are covering here charAt()  method is more detailed manner. String char… Read More
  • Strings in Java : Part 4 String Methods As we know that string class has variety of methods for String manipulation. We would be explaining the mostly used methods available… Read More
  • What is JIRA? We have been hearing about JIRA a lot these day. Aren’t we? So, let us discuss some key pointers on JIRA today. JIRA is a project managemen… Read More
  • What is Apache POI in Selenium testing? In an interview, many times, it has been seen that interviewer asks do you know what is Apache POI? You can explain Apache POI in simple language … Read More
  • GIT Introduction GIT Introduction Any automation testing project, we need to develop around 100 + testcases in the limited span of time. It is impossible if you… Read More

0 comments:

Post a Comment

Popular Posts

Recent Posts



Newsletter

Join our newsletter to get secret tips directly in your inbox!


*We Hate Spam!

Powered by Blogger.