Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconWhat is Action Class in Selenium & It’s Applications?

What is Action Class in Selenium & It’s Applications?

Last updated:
19th May, 2021
Views
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
What is Action Class in Selenium & It’s Applications?

Action Class in Selenium is a built-in feature offered by selenium for managing keyboard and mouse events. It can manage multiple operations and events such as control key, drag and drop, and many more. The Action Class’s operations are implemented using the advanced user interaction API feature of the Selenium Webdriver. 

Check out our free courses to get an edge over the competition.

Commonly used Keyboard and Mouse Events

Special keyboard and mouse events are handled using the Advanced User Interactions API. This API contains the Action and the Actions class that are required while executing these events. Given below are the commonly used keyboard and mouse events provided in the Action class. 

MethodDescription
Click And Hold()To click without releasing at the current mouse location.
context-click()Performs a context-click(Right-click mouse action) at the current mouse location 
doubleClick()Performs a double-click action at the current mouse location
drag and drop(source, target)Performs click-and-hold operation at the source element location, then moves to the target element location and releases the mouse.

Ads of upGrad blog

Parameters:

source-element to emulate button-down events.

Target-element to move and release the mouse  

dragAnd DropBy(source, x-offset, y-offset) Performs click-and-hold at the source element location, moves by a specified offset and then releases the mouse.

Parameters:

source-element to emulate button-down events.

xOffset- to specify horizontal move offset.

yOffset- to specify vertical move offset.

keyDown(modifier_key)Performs the modifier keypress event. Does not release the modifier key with subsequent interactions assuming that it is pressed.

Parameters:

modifier_key to represent any of the modifier keys, such as Keys.SHIFT, Keys.ALT, or Keys.CONTROL.

key Up(modifier_key)Performs a key release.

Parameters:

modifier_key to represent any of the modifier keys, such as Keys.SHIFT, Keys.ALT or Keys.CONTROL.

moveByOffset(x-offset, y-offset)Moves the mouse from the current position by the specified offset. 

Parameters:

x-offset to specify the horizontal offset. A negative value means the mouse is moving to the left.

y-offset to specify the vertical offset. A negative value means the mouse is moving downwards.

moveToElement(toElement)Moves the mouse from the middle to the middle of the element.

Parameters:

element to represent the element to move to.

release()Releases the pressed mouse left mouse button at the current mouse location.
sendKeys(onElement, charsequence)Sends a series of keystrokes on to the element.

Parameters:

Element to represent the element that will get the keystrokes, usually a text field.

Char sequence- any string value to represent the sequence of keystrokes that is sent. 

Examples of Application of the Selenium Action Class

1. Import Action and the Action class.

import org.open a.selenium.interactions.Action;

import org.open a.selenium.interactions.Actions;

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)

2. Instantiate a new Actions object

Actions builder = new Actions(driver);

3. Instantiate an Action using the Action object in the previous step

Action mouseOverHome = builder

.moveToElement(link_Home)

.build();

Here, we are using the moveToElement() method to perform a mouse-over the Home link. The build() is always the final method to ensure that all actions are compiled in a single step.

Explore Our Software Development Free Courses

Check out upGrad’s Java Bootcamp.  

4. Use the perform method while executing the Action object we declared in the previous step.

Syntax:

mouseOverHome.perform();

Example 2:

Let us take another example where the Action class is used to implement the textbox’s Autocomplete feature.

To do this manually, we press the shift key, type the text that must be entered in uppercase and then let go of the shift key. Put, Shift + alphabet keys are pressed together.

To mimic the same operation through automation script, the Actions Class method is utilized. 

1. Import-Package

Action and Actions class exist in the org.openqa.selenium.interactions package of the Web Driver API. To use these you must import packages:

org.openqa.selenium.interactions.Action;

org.openqa.selenium.interactions.Actions;

2. Instantiate the Action class

It would help if you created an object of the Actions class to invoke its methods. So, we instantiate the Actions class and use the WebDriver object to initiate the class. The syntax is:

Actions actions = new Actions (webdriver object);

3. Generate a sequence of actions

In this example, the sequence of steps to perform multiple actions are:

  • Press the Shift key
  • Send the desired text
  • Release the Shift key

To perform these actions, the Action class has methods such as:

  • keyDown method to press the Shift key.
  • sendKeys method to send the desired text.
  • The keyUp method releases the Shift key.
  • The keyDown acts as a modifier key press to focus on an element while the keyUp method releases a modifier key pressed. 

A modifier key changes the action of another key when both the keys are pressed simultaneously. E.g., Shift, Alt and Ctrl.

A sequence of actions is generated but these actions are performed on a webElement. The steps for this are:

WebElement element = driver.findElement;

actions.keyDown(element, Keys, Shift);

action.sendKeys(“TexttoBeConvertAndSendInUpperCase);

actions.keyUp(Keys.Shift);

The important thing to observe here is that if you happen to hover around any Action class method, an Actions class object will be returned.

In-Demand Software Development Skills

4. Build the actions sequence

You can build the actions sequence through the build() method of the Actions class. This method generates the entire set of actions that are ready to be performed. The syntax is:

Action action = actions.build();

The build() method returns an object of type Action that represents a composite action built from a sequence of multiple actions invoked by method calls.

5. Perform action sequences

The last step in executing the Action class is performing the actions sequence through the perform() method of the Action interface. The syntax is:

action.perform();

Ads of upGrad blog

The above-given steps must be executed to leverage all of the Actions class methods and perform different user actions using the keyboard and mouse.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Explore our Popular Software Engineering Courses

What Next?

If you’re interested to learn more about Full-Stack Software development, check out upGrad & IIIT-B’s Executive PG Program Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What is action class in Selenium?

An action class is a class that describes an action, or a series of actions, that you can perform on a web element. We identify the elements we want to perform actions on by locators. Action classes are defined by the two-character combination of a public static class level modifier and an Action suffix (i.e. Action, KeyAction, PageAction, etc.). The public static modifier enables us to expose the class as a class that can be instantiated. The Action suffix denotes the type of action being performed. The Action suffix class is also parameterized. The parameter is the element name of the action. For example, if we want to perform an action on a text field, we would use the parameter textfield.

2What are the uses of Selenium?

Selenium is a very powerful portable software testing tool (selenium software testing tool), which is used to record and playback the user actions on a web page. It is used to test the functional and non-functional requirements of a web application on any web browser. It is implemented in Java and has a very simple API. It supports web applications testing in all the major web browsers like Firefox, Chrome, Internet Explorer, Safari etc.

3How to learn Selenium in Java?

Selenium is a tool that allows you to write automated tests in a number of programming languages. Tests are written using web driver. You can use it to test your web application based on the following programming languages: Java, C#, Python and PHP. Actually, many developers feel that it is not so necessary to learn Selenium Java API. Selenium WebDriver is the right choice to find elements on the page and perform actions with them. It is possible to implement tests in many programming languages, but if you need to write tests in Java, then Selenium WebDriver is the best choice. Another method to learn Selenium in Java is using Selenium IDE, also known as Selenium RC. Browsers support for this tool is not so good as we know, only Firefox and Chrome are supported. But, the IDE can help you to rapidly design and execute Selenium test cases, record and playback test case, auto generate test cases, etc.

Explore Free Courses

Suggested Blogs

Full Stack Developer Salary in India in 2024 [For Freshers & Experienced]
907173
Wondering what is the range of Full Stack Developer salary in India? Choosing a career in the tech sector can be tricky. You wouldn’t want to choose
Read More

by Rohan Vats

15 Jul 2024

SQL Developer Salary in India 2024 [For Freshers & Experienced]
902296
They use high-traffic websites for banks and IRCTC without realizing that thousands of queries raised simultaneously from different locations are hand
Read More

by Rohan Vats

15 Jul 2024

Library Management System Project in Java [Comprehensive Guide]
46958
Library Management Systems are a great way to monitor books, add them, update information in it, search for the suitable one, issue it, and return it
Read More

by Rohan Vats

15 Jul 2024

Bitwise Operators in C [With Coding Examples]
51783
Introduction Operators are essential components of every programming language. They are the symbols that are used to achieve certain logical, mathema
Read More

by Rohan Vats

15 Jul 2024

ReactJS Developer Salary in India in 2024 [For Freshers & Experienced]
902674
Hey! So you want to become a React.JS Developer?  The world has seen an enormous rise in the growth of frontend web technologies, which includes web a
Read More

by Rohan Vats

15 Jul 2024

Password Validation in JavaScript [Step by Step Setup Explained]
48976
Any website that supports authentication and authorization always asks for a username and password through login. If not so, the user needs to registe
Read More

by Rohan Vats

13 Jul 2024

Memory Allocation in Java: Everything You Need To Know in 2024-25
74112
Starting with Java development, it’s essential to understand memory allocation in Java for optimizing application performance and ensuring effic
Read More

by Rohan Vats

12 Jul 2024

Selenium Projects with Eclipse Samples in 2024
43493
Selenium is among the prominent technologies in the automation section of web testing. By using Selenium properly, you can make your testing process q
Read More

by Rohan Vats

10 Jul 2024

Top 10 Skills to Become a Full-Stack Developer in 2024
229999
In the modern world, if we talk about professional versatility, there’s no one better than a Full Stack Developer to represent the term “versatile.” W
Read More

by Rohan Vats

10 Jul 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon