How To Use Mouse and Keyboard Events with Selenium

Using mouse click and keyboard events with Selenium WebDriver

Table of Contents

In this tutorial, we will discuss how to use mouse click events and keyboard events with Selenium WebDriver. The mouse click and keyboard events are used to automate the interactions of a user with a mouse and keyboard.

Action & Actions

We encourage the use of Action and Actions together with the keyboard and mouse events. Action & Actions are user-facing APIs for emulating complex user gestures.

Action is an interface that represents a single user interaction task. Generally,build() method generates a composite action by containing all the actions. On the other hand, Actions implement the builder pattern and it builds a Composite Action that contains all actions specified by method calls.

A Sample Program

Let’s write a simple program explaining these concepts in detail. I’ll select the Facebook website as an example to describe both keyboard and mouse events.

Mouse click event will be triggered when we click the username field. Next, keyboard events will occur when we type the username and password in username and password fields. Again mouseclick event will be used to click on the “Login” button so that we can complete the Facebook login action.

The following image identifies the HTML elements that holds username, password, and log in button.

Accordingly, we can point to all three fields using the ID attribute. Now let’s open the Eclipse IDE and create a new class and name it as Facebook.

Let’s import the libraries.

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

As usual, set the system property to the location of Chrome WebDriver and instantiate a WebDriver element from org.openqa.selenium.

System.setProperty("webdriver.chrome.driver", "D:\\Chromedriver\\chromedriver.exe"); 
WebDriver driver=new ChromeDriver();

Let’s assign the URL for Facebook to the WebDriver element.

driver.get("https://www.facebook.com/");

Next, we will create three new web elements that point to the username, password, and login locations using their IDs.

WebElement username = driver.findElement(By.id("email"));
WebElement password = driver.findElement(By.id("pass"));
WebElement loginButton = driver.findElement(By.id("loginbutton"));

Now, let’s move on to implementing the Action & Actions. We will create a variable called builder from the Actions class. We will use builder to build a composite action that will contain all actions from the method call.

Furthermore, we will create a variable called mouseOver from the Action interface which needs to be used later to run the actions using the build() method.

Actions builder = new Actions(driver);
Action mouseOver = builder
	.moveToElement(username)
	.click()
	.sendKeys(username, "[email protected]")
	.sendKeys(username, Keys.TAB)
	.sendKeys(password, "This is my Password")
	.build();

According to the code above, the moveToElement method will go to the element where the ID is “email” (username box). Later, it will perform the click mouse event so that you can type the username.

Using the sendKeys keyboard event, we will parse the email as a username. Now let’s move to the password box. You can simply achieve this by pressing the tab key from the username box. We will use the same concept here as well. Using the sendKeys keyboard event, we will press the TAB button so that it moves to the next box.

Finally, we will type the password using the sendKeys keyboard event and compile all the actions with the build() method.

We can use the perform() methods to perform the actions which are mentioned below :

mouseOver.perform(); 

Type username and password into the necessary places then we need to click on the login button in order to complete the task. Once again, we will use the mouse click event for the loginButton variable.

loginButton.click();

This method will simply find the ID ‘login button’. Click on top of it. You will see the next logged-in session on the Facebook site, after giving the correct username and password. Confirm the login By pressing the Login button and we can print the current URL of the page after the successful login.

System.out.println(driver.getCurrentUrl());

That’s all with the demonstration. The entire program is as follows:

package myPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class Facebook {
public static void main(String[] args) {  	
         		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe"); 
		WebDriver driver = new ChromeDriver(); 
   		driver.get("https://www.facebook.com/");
         		WebElement username = driver.findElement(By.id("email"));
		WebElement password = driver.findElement(By.id("pass"));
		WebElement loginButton = driver.findElement(By.id("loginbutton"));
         		Actions builder = new Actions(driver);
         		Action mouseOver = builder
			.moveToElement(username)
			.click()
			.sendKeys(username, "This is my Username")
			.sendKeys(username, Keys.TAB)
			.sendKeys(password, "This is my Password")
			.build();
         		mouseOver.perform();  	
         	         	loginButton.click();
         		System.out.println(driver.getCurrentUrl());
   	} 
}

Conclusion

We hope this article helps to understand how mouse and keyboard events work. We hope you apply the same concepts to other webpages and come up with new ideas to automate the keyboard and mouse events with Selenium WebDriver.

Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

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

Recent Posts

RELATED POSTS

Automation Tools Comparison (SilkTest vs QTP vs Selenium)

While manual testing and automated testing go hand in hand, one of the important benefits of automated testing is the assurance that the software...

How To Use TestNG with Selenium

1. What is TestNG? TestNG is an open-source automated testing framework with flexible and powerful features. It is inspired by JUnit and NUnit but with...

How To Find Broken Links with Selenium

What is a broken link? Links are used for navigating between webpages. Users are directed to a web page when they click or type a...

Â

RECENT 'HOW-TO'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string with a function or variable. It does not...

SIMILAR ON CODEX

FEATURED PROJECTS

Windows JDK Manager (win-jdk-Manager)

ADjo LABS PROJECT : Simple and lightweight desktop utility with Interactive cmd Interface for easy view, re-point and switching between JAVA versions on windows. Demonstrating the capability...

MORE IN THIS CATEGORY

Traditional Framework Definitions (QTP)

Over the years Functional Test Automation has come a long way from WinRunner to QTP and until ALM. The tools have evolved to a...

Working with JMeter Listeners

About Listeners Listeners are used for displaying test results in JMeter. Listeners allow system engineers to analyze the responses from the testing system and monetize...

How to View Java API Doc Hints within IntelliJ

IntelliJ Quick Documentation So how do you generally refer your Java API Doc? If you use Google or online Java API Doc or even a...

CHECKOUT TUTORIALS

Java Tutorial #5 – Loop Statements

Iteration statements are used to repeat a particular block of code until a certain condition is true. In this article, we will be taking...
- Advertisement -spot_img