In Selenium, we have Webdriver Element Commands to handle web page interactions like entering text in a text box using sendkeys(), refresh a page using refresh(), etc. Similarly to interact with dropdown, we use Select class with methods like selectbyindex, selectbyvalue, etc.
Suppose, we want to perform complex interactions or keyboard and mouse related interactions like right click, Drag and Drop an element, etc. We need advance API methods to implement these interactions, so Action class appears in place.
What is the Actions class in Selenium?
The user-facing API for emulating complex user gestures. Use this class rather than using the Keyboard or Mouse directly.
Implements the builder pattern: Builds a Composite Action containing all actions specified by the method calls.
Difference between Action and Actions class in Selenium
Actions is a class that 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().
To use Action or Actions class, we need to import below mentioned packages:-
import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.interactions.Action;
How to get an instance of Actions class?
We can create an instance of Actions class simply, by passing current webDriver instance. Now, using the action instance, we can call any of the methods provided in Actions class.
WebDriver driver = new ChromeDriver(); Actions builder = new Actions(driver);
How to see all the supported methods by Actions class?
In order to see all the supported methods by Actions class, press control + space after, Action object (builder)
There is a large number of methods available in Actions class. The below screenshot represents some of those methods
Methods in Actions class of Selenium
There are many methods in this class, which can categorized into two main categories:
· Keyboard Events
· Mouse Events
Different Methods for performing Keyboard Events:
1) keyDown(java.lang.CharSequence key) – Performs a modifier key press
2) keyUp(java.lang.CharSequence key) -Performs a modifier key release
3) sendKeys(java.lang.CharSequence… keys) -Sends keys to the active element.
Different Methods for performing Mouse Events:
1) click() – Clicks at the current mouse location.
2) clickAndHold() – Clicks (without releasing) in the middle of the given element.
3) contextClick() – Performs a context-click at middle of the given element.
4) doubleClick() – Performs a double-click at the current mouse location.
5) dragAndDrop(WebElement source, WebElement target) – A convenience method that performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
6) dragAndDropBy(WebElement source, int xOffset, int yOffset) – A convenience method that performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
7) moveByOffset(int xOffset, int yOffset) – Moves the mouse from its current position or (0,0) by the given offset.
8) MoveToElement(WebElement target) – Moves the mouse to the middle of the element.
9) perform() – A convenience method for performing the actions without calling build() first.
10) release() – Releases the depressed left mouse button at the current mouse location.