Actions Class in Selenium

Introduction

Selenium is a testing tool that is widely used to test web applications. It provides with lot of in built functionalities that can be used readily to automate a web application, no matter how complex the UI is. Selenium provides different classes that contains multiple methods which we can use to perform different operations on a web application. This makes testers life easier and allows them to test faster and more efficiently without having to write lengths of code.

‘Actions’ class is one such component of Selenium that provides multiple methods to perform different actions in the web browser. Read on to know more about it.

What is Actions class in Selenium

Automating a web application involves clicking of buttons, enter text in a text field and many such actions. These interactions are done through keyboard and mouse. Such interactions include drag and drop, clicking on multiple elements using control key. This is where Actions class can be used. Actions class provides methods that can used to perform keyboard and mouse actions in a web application.

Actions class is defined and invoked using the following syntax:

Actions action = new Actions(driver);

action.moveToElement(element).click().perform();

In python, the class used to perform complext keyboard and mouse actions is ‘ActionChains’.

Methods of Actions Class / Actions Facilitated by Actions class

Action class is used to perform keyboard and mouse events in a web application. It provides with multiple methods that help to achieve this.

Some of the actions that can be performed using Actions class are as below.

Mouse Actions:

  • doubleClick(): Performs double click on the element
  • clickAndHold(): Performs long click on the mouse without releasing it
  • dragAndDrop(): Drags the element from one point and drops to another
  • moveToElement(): Shifts the mouse pointer to the center of the element
  • contextClick(): Performs right-click on the mouse

Keyboard Actions:

  • sendKeys(): Sends a series of keys to the element
  • keyUp(): Performs key release
  • keyDown(): Performs keypress without release

Difference between Action class and Actions interface

Actions is a class that is based on a builder design pattern.  This is a user-facing API for emulating complex user gestures.

Whereas Action is an Interface which represents a single user-interaction action. It contains one of the most widely used methods perform().

How to use Action class?

Now we have seen what is Actions class. Let us see how it can be implemented to test web applications.

Step 1: First, we have to import the package org.openqa.selenium.interactions.Actions.

Step 2: To use the methods provided by the Actions class, we need to create an object of this class and pass the WebDriver as an argument.

// instantiate the WebDriver

WebDriver driver = new ChromeDriver();

// create an object of the Actions class

Actions act = new Actions(driver);

Step 3: The object created can now be used to perform any actions. You can see various actions that are provided by this class once you create an object.

An example of using Actions Class method

sendKeys()

Using the Actions class in Selenium, the user can implement the sendKeys() method to type specific values in the application. Below is a simple code which is used to search a product in a search engine by passing the product’s name in the search box.

 

That is how to use the actions class in Selenium with sendKeys() method. The build() method generates a composite action containing all actions which are ready to be performed. The perform() method is used to perform the series of actions that are defined.

Summary

The Selenium Actions class is useful for performing actions on any element on the screen by specifying x and y coordinates. It is possible to locate more than one web element using the Actions class.  This article simplifies the process so that testers know how to simulate common user actions on websites and applications. This lets them monitor software behavior in the real world so that they can verify and optimize user experience for its best possible state.

 

Contact us at sales@brainbox.consulting for more information and we would be happy to assist you with testing related services.

Leave a comment

You must be logged in to post a comment.