As we know the user can perform different action on web pages in order to interact with the web elements and that can be achieved by WebDriver API Commands like clicking on a button using click() method, entering text in text box using sendKeys(), etc. Also if we want to handle dropdown list , WebDriver provides Select Class which has different methods like selectByValue(), deselectAll(), etc.
Lets consider a Scenario that you need to perform double click on any web element, drag and drop elements or send Upper Case letter using Shift+Letters on any text box element then to perform these more complicated interactions we use Actions Class.
So if we want to Handle special keyboard and mouse events then we use the Advanced User Interactions API. It contains the Actions and the Action classes that are needed when executing these advanced Keyboard and mouse events.
There are lot of methods in Actions Class, but based on action we categorized those methods into –
1. Keyboard Actions
2. Mouse Actions
Lets see some frequently use methods for these actions below.
Methods for Keyboard Actions –
keyDown(modifierKey): Press the keysendKeys(keysToSend ):Sends keys to the active web elementkeyUp(modifierKey): Release the key
Here (modifierKey) can be any of the modifier keys like (Keys.ALT, Keys.SHIFT, or Keys.CONTROL)
Methods for Mouse Actions –
click(): Clicks at the current mouse location.doubleClick(): Performs a double-click at the current mouse location.contextClick(): Performs a context-click (Right Click Mouse Action) at middle of the given element.clickAndHold(): Clicks (without releasing) in the middle of the given element.dragAndDrop(source, target): Click-and-hold at the location of the source element, moves to the location of the target elementdragAndDropBy(source, x-Offset, y-Offset): Click-and-hold at the location of the source element, moves by a given offsetmoveByOffset(x-offset, y-offset): Moves the mouse from its current position (or 0,0) by the given offsetmoveToElement(toElement): Moves the mouse to the middle of the elementrelease(): Releases the depressed left mouse button at the current mouse location
Let’s see some example where we use Actions class to perform our desired actions on web elements –