Handling Keyboard & Mouse Events
Handling special keyboard and mouse events are done using the Advanced User Interactions API. It contains the Actions and the Action classes that are needed when executing these events. The following are the most commonly used keyboard and mouse events provided by the Actions class.
Parameters: source- element to emulate button down at. target- element to move to and release the mouse at. Parameters: source- element to emulate button down at. xOffset- horizontal move offset. yOffset- vertical move offset. Parameters: modifier_key – any of the modifier keys (Keys.ALT, Keys.SHIFT, or Keys.CONTROL) Parameters: modifier_key – any of the modifier keys (Keys.ALT, Keys.SHIFT, or Keys.CONTROL) Parameters: x-offset- horizontal offset. A negative value means moving the mouse left. y-offset- vertical offset. A negative value means moving the mouse down. Parameters: toElement- element to move to. Parameters: onElement – element that will receive the keystrokes, usually a text field charsequence – any string value representing the sequence of keystrokes to be sent
The cell shown above is a portion of a
Step 2: Instantiate a new Actions object.
Step 3: Instantiate an Action using the Actions object in step 2.
In this case, we are going to use the moveToElement() method because we are simply going to mouse-over the “Home” link. The build() method is always the final method used so that all the listed actions will be compiled into a single step. Step 4: Use the perform() method when executing the Action object we designed in Step 3.
Below is the whole WebDriver code to check the background color of the
Building a Series of Multiple Actions
You can build a series of actions using the Action and Actions classes. Just remember to close the series with the build() method. Consider the sample code below.
Summary
Handling special keyboard and mouse events are done using the AdvancedUserInteractions API. Frequently used Keyword and Mouse Events are doubleClick(), keyUp, dragAndDropBy, contextClick & sendKeys.