While performing manual testing, we perform various mouse and keyboard events which can easily be automated by the use of Selenium-based commands. Selenium provides multiple methods to handle all mouse and keyboard events with advanced user interactions API.
Before moving ahead with keyboard and mouse events, let’s have a look at the difference between Action and Actions class in Selenium.
Since Action and Actions class sounds same, testers gets confused with their working. Basically, Action is not a class but an interface. Actions class implements the Action interface.
Action interface represents the single user interaction action and is used to compile all the actions defined by Actions class. Action interface provides a method perform () to perform or execute the defined actions. Actions class is based on builder design pattern for emulating complex user gestures.
In order to use Action interface and Actions class, we need to import the following libraries:
Syntax of defining Actions class:- Actions ActionsObject = new Actions(WebDriverInstance)
There are basically two methods in Actions class that can be further categorized with different kinds of actions in Selenium:
It accepts modifier key as a parameter and performs press action until specified to release the mentioned Key. The standard modifier keys are Keys.SHIFT, Keys.Control, Keys.TAB etc.
Syntax: keyDown(WebElement obj, Keys.modifierKey) or keyDown(Keys.modifierKey)
It accepts modifier key as a parameter and performs key release action for a specified mentioned Key.
Syntax: keyUp(WebElement obj, Keys.modifierKey) or keyUp(Keys.modifierKey)
This method is used to send a sequence of characters to the defined input field. The use of sendKeys() with Actions class maintains the focus of webdriver on that particular input field and is helpful when using modifier keys.
Click at the current mouse position or at the element specified in the parameters until determined to release.
Used to perform right-click or context-click operation on the element specified.
Used to perform double click events on the element specified.
Used to drag an element from the source location and drop it to the target location.
Used to drag an element from the source location and drop it to the target location mentioned in the form of coordinates.
Used to move the current mouse position to the specified coordinates position.
Used to move the current mouse position to the specified web element.
Releases the left mouse button at the position specified.
Output of first test annotation method test_func1():
NOTE : In the above code we have called build () method that resets the internal builder state to call actions in sequence by compiling all the actions, while the perform () method is used to deliver or execute the actions.
To know more about the Actions class, check out the Selenium official website.
We as a Software Testing Services Providers have now covered all the keyboard and mouse events that can be handled using Selenium WebDriver. The above code example and theory can help you with advanced user interaction API using Actions class. So, give it a try by running the above code, every test annotation method have different output depending on the events they perform.
Good Luck, Happy Testing!!
Read more about 7 Tasks those are Helpful to Software Testers