JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

Handling Mouse and Keyboard Events in Selenium WebDriver

Introduction

While performing manual testing, we perform various mouse and keyboard events which can easily be automated by the use of Selenium-based commands. Selenium provides multiple methods to handle all mouse and keyboard events with advanced user interactions API.

Before moving ahead with keyboard and mouse events, let’s have a look at the difference between Action and Actions class in Selenium.

Since Action and Actions class sounds same, testers gets confused with their working. Basically, Action is not a class but an interface. Actions class implements the Action interface.

Action interface represents the single user interaction action and is used to compile all the actions defined by Actions class. Action interface provides a method perform () to perform or execute the defined actions. Actions class is based on builder design pattern for emulating complex user gestures.

In order to use Action interface and Actions class, we need to import the following libraries:

actions in selenium

Syntax of defining Actions class:- Actions ActionsObject = new Actions(WebDriverInstance)

Mouse and Keyboard Events

mouse and keyboard events

There are basically two methods in Actions class that can be further categorized with different kinds of actions in Selenium:

Keyboard interface methods

Mouse interface methods

Sample Code Example Covering All Keyboard And Mouse Interface Methods:

package MyPackage; import java.util.concurrent.TimeUnit; 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.Actions; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class MouseandKeyboard { WebDriver driver; @BeforeMethod public void setUp() { System.setProperty("webdriver.chrome.driver", "C:\\Users\\Lenovo-I7\\Desktop\\chromedriver.exe"); driver = new ChromeDriver(); } @Test(priority = 1) public void test_func1() throws InterruptedException { driver.get("http://www.google.com/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); WebElement element = driver.findElement(By.name("q")); Actions action = new Actions(driver); action.moveToElement(element) .keyDown(Keys.SHIFT) .sendKeys("Selenium Webdriver") .keyUp(Keys.SHIFT) .doubleClick() .contextClick() .build() .perform(); Thread.sleep(5000); } @Test(priority = 2) public void test_func2() throws InterruptedException { driver.get("https://jqueryui.com/droppable/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.switchTo().frame(0); WebElement holdObject = driver.findElement(By.id("draggable")); WebElement releaseHere = driver.findElement(By.id("droppable")); Actions action = new Actions(driver); action.clickAndHold(holdObject) .moveToElement(releaseHere) .release() .build() .perform(); Thread.sleep(7000); } @Test(priority = 3) public void test_func3() throws InterruptedException { driver.get("https://jqueryui.com/draggable/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.switchTo().frame(0); WebElement holdObject = driver.findElement(By.id("draggable")); Actions action = new Actions(driver); action.dragAndDropBy(holdObject, 370, 250) .build() .perform(); Thread.sleep(7000); } @Test(priority = 4) public void test_func4() throws InterruptedException { driver.get("https://google.com"); driver.manage().window().maximize(); Actions action = new Actions(driver); action.moveByOffset(700, 365).contextClick() .build() .perform(); Thread.sleep(7000); } @AfterMethod public void tearDown() { driver.quit(); } }

Output of first test annotation method test_func1():

method test func1

NOTE : In the above code we have called build () method that resets the internal builder state to call actions in sequence by compiling all the actions, while the perform () method is used to deliver or execute the actions.

To know more about the Actions class, check out the Selenium official website.

Conclusion

We as a Software Testing Services Providers have now covered all the keyboard and mouse events that can be handled using Selenium WebDriver. The above code example and theory can help you with advanced user interaction API using Actions class. So, give it a try by running the above code, every test annotation method have different output depending on the events they perform.

Good Luck, Happy Testing!!

 

Some of our clients

team