Selenium WebDriver – Handle Keyboard And Mouse Events

Keyboard action such as key press is a must have action in some automation test cases. And sometimes you even need to press different keys in your selenium webdriver automation test scripts to make a menu popup. Or you need to simulate click Enter key to submit a form. And the mouse events such as mouse over,  out and hover actions is also very important in selenium webdriver automation test. How to do those in selenium automation test scripts. In this article we will tell you how to achieve that.

Webdriver’s Actions class provide a lot of useful methods for us to implement keyboard and mouse actions automatically in selenium webdriver test scripts. We can use those methods to simulate key press, mouse hover and even drag and drop actions.

Keyboard Press Example

You can use WebElement’s sendKeys method to send a keyboard action to current selected web element.

 @Test
 /* Press Enter key to submit the search form in this method. */
 public void testKeyboardAction() throws InterruptedException
 {
 /* Go to bing.com. */
 this.ffDriver.get("http://bing.com");
 
 Thread.sleep(3000);
 
 /* Find the bing search keyword input text box. */
 By byIdSearchKeyword = By.id("sb_form_q");
 WebElement queryElement = this.ffDriver.findElement(byIdSearchKeyword);
 
 /* Input keyword "java" in the text box. */
 queryElement.sendKeys("java");
 
 Thread.sleep(3000);
 
 /* Click Enter key to submit the search. */
 queryElement.sendKeys(Keys.ENTER);
 
 Thread.sleep(3000);
 }

Mouse Hover Example

You need use following code to implement mouse hover actions. Please notice the moveToElement method and it’s usage.

/* Create the actions object. */
 Actions action = new Actions(ffDriver);
 
 /* Focus on the Departments web element.
 * Then all the sub product categories menu list will pop up.
 * This method implements the mouse hover action.
 * */ 
 action.moveToElement(departmentsElement).build().perform();

Below is the steps of the java example code. You can read the detail explanation in java code comments.

  1. Go to amazon.com
  2. Mouse hover on the Departments menu.
  3. Mouse hover on the “Electronics & Computers”.
  4. Find and click the “Computers & Tablets” menu link.
  5. Show the “Computers & Tablets” product page.
  @Test
 /* Test mouse hover action. */
 public void testMouseHoverAction() throws InterruptedException
 {
 /* Go to amazon.com. */
 this.ffDriver.get("http://www.amazon.com");
 
 Thread.sleep(3000);
 
 /* Find amazon Departments ( all product category ) menu. */
 By byIdDepartments = By.id("nav-link-shopall");
 WebElement departmentsElement = this.ffDriver.findElement(byIdDepartments);
 
 /* Create the actions object. */
 Actions action = new Actions(ffDriver);
 
 /* Focus on the Departments web element.
 * Then all the sub product categories menu list will pop up.
 * This method implements the mouse hover action.
 * */ 
 action.moveToElement(departmentsElement).build().perform();
 
 Thread.sleep(3000);
 
 /* Find and move to the 'Electronics & Computers' sub menu item inside the Departments menu. */
 By byXPathIT = By.xpath("//span[text()='Electronics & Computers']");
 WebElement itElement = departmentsElement.findElement(byXPathIT);
 action.moveToElement(itElement).build().perform();
 
 Thread.sleep(3000);
 
 /* Find and click the 'Computers & Tablets' sub menu item inside the 'Electronics & Computers' menu. 
 * Then goto the 'Computers & Tablets' products page.
 * */
 By byXPathPC = By.xpath("//span[text()='Computers & Tablets']");
 WebElement pcElement = itElement.findElement(byXPathPC);
 pcElement.click(); 
 
 Thread.sleep(10000);
 }

[download id=”852″]

Subscribe to receive more programming tricks.

We don’t spam!

Subscribe to receive more programming tricks.

We don’t spam!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.