Actions Class in Selenium

Selenium is an automation tool widely used for automating web applications. Selenium differentiates itself from other tools by providing many ways to test web applications. Selenium offers multiple options for automating even applications with a very complex UI and hence wipes out the need for any manual effort.

Now, let us see what action class is in Selenium. In several instances, there is a need for web applications to interact with the browser through a mouse or keyboard. In order to automate these inputs from the mouse and keyboard, Action Class in Selenium is used. Action Class in Selenium provides different methods to perform multiple or single actions in the browser. Actions like hovering, button clicks, and entering keywords in the search bar are examples of how we use a mouse and keyboard.

Syntax: Actions actions = new Actions(driver);

Mouse Actions

The various mouse actions provided by the Actions Class are:

  1. click - clicks at the current mouse location
  2. doubleClick - double clicks at the current mouse location 
  3. contextClick - performs a right click at the present mouse location
  4. dragAndDrop - drags an element from a source location to a target location
  5. moveToElement - moves to the target element

Keyboard Actions

The various keyboard actions provided by the Actions Class are:

  1. sendKeys - sends a series of key presses to an element
  2. keyDown - holds down the key
  3. keyUp - releases the key

Now, let us take a look at some of these methods in Selenium in detail.

  • Sendkeys() - It is a method that sends inputs from the keyboard to a web page. It simulates keyboard events like holding, pressing, and releasing the keys.

Syntax: actions.sendKeys(element, "Hello, World!").perform();

  • MouseClick() - This method simulates different mouse clicks on an element present on a web page. It basically performs different mouse-click events such as clicking a button or a link.

Syntax: actions.click().perform();

  • MoveToElement - It moves the cursor of the mouse to the required position in a web page, that is by simulating movements of the mouse cursor, we can perform actions like click, hover, drag-and-drop. This comes in handy for testing UI elements like tooltips, dropdowns, etc.

Syntax: actions.moveToElement(element).perform();

  • ContextClick - This performs a right-click action on a web page element. This is helpful when testing for context menus that appear on the right click.

Syntax: actions.contextClick().perform();

  • DoubleClick - This performs a double click on any web page element such as a file or folder in a file explorer.

Syntax: actions.doubleClick().perform();

  • DragandDrop - This method simulates the action of dragging and dropping elements in a web page such as sorting elements or moving an element from one place to another.

Syntax: actions.dragAndDrop(sourceElement, targetElement).perform();

  • KeyUp/KeyDown - They simulate the pressing and releasing actions of the keyboard keys. Useful for testing form fields and game controls.

Syntax: actions.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTRO).perform();

In conclusion, the Actions Class in Selenium provides a powerful set of methods for automating mouse and keyboard interactions with web applications. It offers flexibility and versatility in performing various actions, making it an indispensable tool for web automation testing.