Complex Operations with Selenium WebDriver Actions API

This tutorial on WebDriver Actions API is Part 11 of the Selenium WebDriver Tutorial Series. Check out Part 10 to learn about Image Comparison.

Actions API

WebDriver Actions API helps us in emulating complex user gestures like click and hold, context click, hover, double click, drag and drop etc.

WebDriver Actions API

First, we will look at the general approach of using Actions API and then cover each operation in detail.

The first step is initializing the Actions object as shown below.

Then perform the operations like so.

Get the Action object and execute it.

Now lets see each operation in detail

Click Operations

  • click() : Clicks at the current mouse location.
  • click(WebElement onElement) : Clicks at the given element
  • clickAndHold() : Click and hold on the current mouse location
  • clickAndHold(WebElement onElement) : Click and hold on the given element
  • doubleClick() : Double click on the current mouse location
  • doubleClick(WebElement onElement) : Double click on the given element
  • release() : releases the mouse button at the current location
  • release(WebElement onElement): releases the mouse button at the given element

Context Click / Right Click

  • contextClick() : Context Click (Right Click) on the current location
  • contextClick(WebElement onElement): Context Click (Right Click) on the given element

Mouse Movement Operations

  • dragAndDrop(WebElement source, WebElement target): Click and holds on the source, move to the target and releases.
  • dragAndDropBy(WebElement source, int xOffset, int yOffset) : Click and holds on the source, move to the given offset and releases.
  • moveByOffset(int xOffset, int yOffset) : Moves the move by the given offset
  • moveToElement(WebElement toElement) : Moves to the given element
  • moveToElement(WebElement toElement, int xOffset, int yOffset) : Moves the mouse to an offset from the top-left corner of the element.

Keyboard Operations

  • keyDown(Keys theKey) : Performs Key press
  • keyDown(WebElement element, Keys theKey) : Performs Key press on given element
  • keyUp(Keys theKey) : Performs Key release
  • keyUp(WebElement element, Keys theKey) : Performs Key release on given element
  • sendKeys(java.lang.CharSequence… keysToSend) : Types on focused element
  • sendKeys(WebElement element, java.lang.CharSequence… keysToSend) : Types on given element

Actions API Examples

Lets see some of the examples where we would need Actions API

SEE ALSO:   Selenium WebDriver Framework: Creating Executor

Consider these scenarios:

  • Select multiple elements by clicking with Control or Shift key pressed
  • Typing on element with Shift or any other key pressed down
  • Select multiple elements (by having Ctrl/Shift pressed) and drag them to another element.
  • Hover to an element and click another element
Similarly, you can generate your own complex actions.

You may also like...

Leave a Reply

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