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

Top 7 Node js Project Ideas & Topics
31373
Node.JS is a part of the famous MEAN stack used for web development purposes. An open-sourced server environment, Node is written on JavaScript and he
Read More

by Rohan Vats

05 Mar 2024

How to Rename Column Name in SQL
46787
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers & Experienced]
901145
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners & Experienced]
51370
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers & Experienced]
908708
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34595
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers & Experienced]
902276
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
25886
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions & Answers [2024]
4032
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

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