Selenium Webdriver Tutorials

Keyboard and Mouse Actions in Selenium Webdriver

What are the common ways you interact with an application? Yeah! The keyboard and mouse. Almost everything we perform in an application is via keyboard or a mouse – click, double-click, type, drag, mouse hover, right-click, etc. So here comes an obvious question for automation testing – How do you replicate (or automate) keyboard and mouse actions in Selenium Webdriver? As you might have guessed, every automation tool provides a way to script keyboard and mouse events. Selenium Webdriver provides an Advanced User Interaction API (including Actions class) which facilitate user actions to be performed in an application, i.e. users can use this API to simulate keyboard and mouse actions in Selenium Webdriver.

Keyboard and Mouse Actions in Selenium Webdriver

When using Selenium Webdriver, the control of browser and the application is with the reference variable of type WebDriver, i.e. the WebDriver reference variable can identify any web element present on the page. But it doesn’t have the ability to handle all the keyboard & mouse events like right-click, drag & drop, etc. Therefore WebDriver reference variable alone is NOT sufficient to automate every user action. But don’t worry! As stated earlier, Selenium Webdriver provides an Advanced User Interaction API (including Actions class) which facilitate keyboard and mouse actions.

import org.openqa.selenium.interactions.Actions;

Actions class in Selenium Webdriver

The Actions class is a user-facing API for emulating complex keyboard and mouse actions in Selenium Webdriver. You can directly use this class rather than using the input devices, i.e. Keyboard or Mouse. It Implements builder pattern, in which a complex object is constructed, i.e. builds a “Composite Action” containing all actions specified by the individual method calls. For e.g. Composite Action: Mouse Hover >> Click.

Selenium Webdriver Actions class

public class Actions

extends java.lang.Object

Default constructor below uses the default keyboard, mouse implemented by the driver.

Actions(WebDriver driver)

  1. Import the package to access the actions class methods.
  2. Create the actions class object
  3. Access the range of available methods.

// Create the Actions class object
Actions builder = new Actions(driver);

// To focus (mouse hover) on element
builder.moveToElement(element).perform();

Note:  Perform method is used here to execute the action.

Execute multiple actions using build()

Above we simply performed one action on the element (mouse hover), in case we want to perform sequence of events on the same element, it can be done using build method.

// To focus and then click on the element
builder.moveToElement(element).click().build().perform();

Keyboard and Mouse Actions in Selenium Webdriver

Mouse Actions in Selenium Webdriver

Below actions help in simulating mouse events in case of automation using Selenium Webdriver.

  • public Actions click() — Clicks at the current mouse location. Useful when combined with 
  • public Actions click(WebElement onElement) — Clicks in the middle of the given element (onElement).
  • public Actions clickAndHold() — Clicks (without releasing) at the current mouse location.
  • public Actions clickAndHold(WebElement onElement) — Clicks (without releasing) in the middle of the given element (onElement)
  • public Actions contextClick() — Performs a context-click at the current mouse location.
  • public Actions contextClick(WebElement onElement) — Performs a context-click at middle of the given element (onElement)
  • public Actions doubleClick() — Performs a double-click at the current mouse location.
  • public Actions doubleClick(WebElement onElement) — Performs a double-click at middle of the given element (onElement)
  • public Actions 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.
  • public Actions 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.
  • public Actions moveByOffset(int xOffset, int yOffset) — Moves the mouse from its current position (or 0,0) by the given offset. If the coordinates provided are outside the viewport (the mouse will end up outside the browser window) then the viewport is scrolled to match.
  • public Actions moveToElement(WebElement toElement) — Moves the mouse to the middle of the element. The element is scrolled into view and its location is calculated using getBoundingClientRect.
  • public Actions moveToElement(WebElement toElement, int xOffset, int yOffset) — Moves the mouse to an offset from the top-left corner of the element. The element is scrolled into view and its location is calculated using getBoundingClientRect.
  • public Actions release() — Releases the depressed left mouse button at the current mouse location.
  • public Actions release(WebElement onElement) — Releases the depressed left mouse button, in the middle of the given element. Invoking this action without invoking clickAndHold()first will result in undefined behaviour.

Keyboard Actions in Selenium Webdriver

keyDown and keyUp are the two main methods available under Selenium Webdriver Actions class.

  • public Actions keyDown(Keys theKey) — Performs a modifier key press (SHIFT,Keys.ALT or Keys.CONTROL). Does not release the key – subsequent interactions may assume it’s kept pressed. Note that the key is never released implicitly – eitherkeyUp(theKey) or sendKeys(Keys.NULL) must be called to release.
  • public Actions keyDown(WebElement element, Keys theKey) — Performs a modifier key press (SHIFT,Keys.ALT or Keys.CONTROL) after focusing on an element.
  • public Actions keyUp(Keys theKey) — Performs a modifier key release (SHIFT,Keys.ALT or Keys.CONTROL). Releasing a non-depressed modifier key will yield undefined behaviour.
  • public Actions keyUp(WebElement element, Keys theKey) — performs a modifier key release after focusing on an element.
  • public Actions sendKeys(java.lang.CharSequence… keysToSend) — Sends keys to the active element. This differs from calling sendKeys(CharSequence…) on the active element in two ways: The modifier keys included in this call are not released and there is no attempt to re-focus the element. So sendKeys(Keys.TAB) for switching elements should work.
  • public Actions sendKeys(WebElement element, java.lang.CharSequence… keysToSend) — Sends keys to the given element.

Generic Actions

  • public Action build() — Generates a composite action containing all actions so far, ready to be performed. It resets the internal builder state, so subsequent calls to build() will contain fresh sequences.
  • public Actions pause(long pause) — Performs a pause. It’s deprecated since‘Pause’ is considered to be a bad design practice.
  • public void perform() — A convenience method for performing the actions without calling build() first.

I know all these methods together will be too much to grab as of now. But don’t worry! In subsequent articles we will learn about the commonly used keyboard and mouse actions in Selenium Webdriver. Till then – Stay tuned – And keep sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *