Introduction
Actions class is a built-in ability to handle various types of keyboard and mouse events.
Selenium provides multiple options to automate it thereby reducing or completely wiping away the manual effort.
It includes various operations such as clicking on multiple elements with the help of control key, drag and drop events and many more are done using the advanced user interactions API.
The actions that can be performed in a browser are broadly classified into two categories namely-
1. Mouse actions
2. Keyboard actions
Mouse Actions
The various mouse actions that are provided by the Actions class are-
click() – clicks at the current location
doubleClick() – performs a Double click at the current mouse location
contextClick() – performs a Right click at the current mouse location
dragAndDrop(WebElement source, WebElement target) – drags an element from the source location and drops in target location
dragAndDropBy(source, xOffset, yOffset) - Performs click-and-hold at the source location, shifts by a given offset value, then frees the mouse. (X offset – to shift horizontally, Y Offset – to shift vertically).
moveToElement(WebElement target) – moves to the target element
Keyboard Actions
The various keyboard actions that are provided by the Actions class are-
sendKeys (keysToSend) – Sends a series of keystrokes onto the element.
keyDown – performs a key press without release.
(For Example: Keys.ALT, Keys.SHIFT, or Keys.CONTROL)
3. keyUp – performs a key release.
How to handle action class in selenium?
1. Import actions class
2. Create an object of the Actions class ‘action‘
3. We can use the different methods under the actions class to perform various operations like drag and drop, click and so on.
Few examples for the implementation of the action class method.
Drag and drop using source and target (Mouse actions)
code snippet:
Output for the above code
Drag and drop using source , x offset and y offset (Mouse actions)
code snippet:
Output for the above code
Converting Texts to Uppercase (Keyboard actions)
Code snippet:
Output for the above code
Hope you guys enjoyed reading this article and understood about the actions and the different methods under these actions class in Selenium.