Actions class (Selenium): A quick look on keyboard & mouse events

You must have heard about Actions class in Selenium. Actions class is the heart of Selenium which allows us to perform advanced activities. So if you are a newbie then you may ask, what are the advanced activities in Selenium?

Hold on my dear friends, you just need to devote 10 minutes of your life on this article. Trust me, after 10 minutes you gonna feel like the Superman of the Actions class for Selenium advanced activities.

Are you laughing?

Well, let’s understand the core concept behind Actions class.

What is Actions Class in Selenium?

Actions class is an API which handles user interactions with the web application. User interactions are done using the keyboard as well as mouse events; hence, Actions class provided us methods to handle all the operations which we perform on any web pages by using keyboard and mouse.

I should not tell you how much this API keep importance to any Automation engineer.

Can you list down the user interactions that we can perform using Actions class?

This could be your next question in your mind, right?

I’d be happy to answer all your queries. So let me tell you the advanced user activities which we can perform using Actions class on any web application. So here we go!

Right-click, Mouse Hover, Pressing any keys on the keyboard, Double click, Click and Hold, Releasing the pressed keys on the keyboard, Scrolling, Drag and Drop, etc. are the advanced activities which we use to perform using Selenium’s Actions class. These activities lower the burden of complex codes of the programming languages. We just need to call the methods for the respective activities and that’s it.

Actions class in Selenium

To use Actions class, you just need to import the following:

org.openqa.selenium.interactions.Actions

Once you import Actions class by providing above path then you will be able to use all the methods which are included in its library. You just need to pick one based on the requirement of the scenario.

After giving a path to the package, we need to create an instance of the Actions class then we pass an argument of WebDriver in it. We do this to continue our Selenium Automation testing process.

You all know we have discussed everything about Actions class which are used in handling various scenarios. Here is the list of functionalities we discussed; they are very helpful in writing your Selenium code.

These are the functionalities which we have discussed in past. Let me tell you few additional methods which are available in Actions class. These methods have their own significance.

click()

It allows WebDriver to click on the current mouse location. Its declaration will be same as earlier. Means first create an instance of actions class and proceed further by calling the method.

click(WebElement target_element)

It allows WebDriver to click on specific web element. It clicks on the middle of the element by default.

clickAndHold()

It clicks on the web element without releasing at the current mouse location.

clickAndHold(WebElement target_element)

It also clicks on the web element without release at the specific web element. It, by default, clicks on the middle of the element.

build()

It is like creating a build in DevOps. It generates the composition of actions which is ready to be performed.

perform()

This is an action which is ready to be performed without calling the build. But we use build & perform together.

release()

This method will be used after clickAndHold(). It helps to release the mouse which is depressed at current mouse location.

release(WebElement target_element)

It performs the same operation as its previous method, but it chooses to work on specific web element. By default, it clicks on the middle of the element.

pause(java.time.Duration duration) and pause(long pause)

It pauses the current state of execution by giving time delay. It is similar to Thread.sleep().

I believe all your confusions related to Actions class should be answered. If you still have any question, please feel free to post a comment below.

Reference: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html

Leave a Reply