Action Class in Selenium

Learn via video courses
Topics Covered

Overview

Popular open-source automation tool Selenium is used to test web applications. To interact with web pages, it offers a wide range of functionality, such as manipulating web components, mimicking user actions, and running JavaScript code. The Action class in Selenium is one of the major Selenium classes, enabling you to carry out complex activities on web pages, including mouse and keyboard events. How to use Action Class in Selenium is described in this article.

Introduction

A variety of classes and techniques are available with Selenium to automate web application testing. One of the key Selenium classes that enables you to carry out complex actions on web pages is the Action class. To test web applications, the Action class offers a means of simulating user activities including mouse clicks, mouse hovers, and keyboard events.

What is Action Class in Selenium?

You may conduct several user actions on web items using the built-in Action class in Selenium, including mouse clicks, mouse hovers, and keyboard events. It is utilized to create complex activities that incorporate numerous user events and is a component of the org.openqa.selenium.interactions package. By using the Action class, testers can create more complex and realistic test scenarios that more closely mimic user behavior. For example, you can use the Action class to simulate a user dragging and dropping an element on a web page or to simulate a user hovering over a particular element.

Methods of the Action Class

The following are the most commonly used Action Class Methods in Selenium WebDriver:

Mouse Actions in Selenium

Mouse actions are an essential part of web automation testing. Action Class in Selenium allows us to perform various mouse actions like clicking, double-clicking, and right-clicking.

click()

  • The click() method is used to simulate a left-click on the web element.

  • Example:

doubleClick()

  • The doubleClick() method is used to simulate a double click on the web element.
  • Example:

clickAndHold()

  • The clickAndHold() method is used to simulate a left-click and hold on the web element.
  • Example:

release()

  • The release() method is used to release the mouse button on the web element after clickAndHold() is performed.
  • Example:

dragAndDrop()

  • The dragAndDrop() method is used to simulate the dragging and dropping of a web element.
  • Example:

moveToElement()

  • The moveToElement() method is used to move the mouse to a web element.
  • Example:

contextClick()

  • The contextClick() method is used to simulate a right-click on the web element.
  • Example:

Keyboard Actions in Selenium

Keyboard actions are an essential part of web automation testing. Action Class Methods in Selenium allow us to perform various keyboard actions like typing, pressing keys, and releasing keys.

sendKeys():

  • The sendKeys() method is used to simulate keyboard input to a web element.
  • Example:

keyUp():

  • The keyUp() method is used to release a pressed key on the keyboard.
  • Example:

keyDown():

  • The keyDown() method presses a key on the keyboard.
  • Example:

Examples of Action Class in Selenium

Here are a few examples of common tasks that we can do using Action Class Methods in Selenium

Perform Click Action on the Web Element

  • Consider the following HTML code snippet:
  • To perform a click action on the Submit button using the Action class in Selenium, use the following code snippet:

Perform Mouse Hover Action on the Web Element:

  • Consider the following HTML code snippet:
  • To perform a mouse hover action on the Products menu using the Action class in Selenium, use the following code snippet:

Perform Double Click Action on the Web Element:

  • Consider the following HTML code snippet:
  • To perform a double-click action on the text input field using the Action class in Selenium, use the following code snippet:

Conclusion

  • The Action class in Selenium is a powerful tool that enables you to perform complex user actions on web elements.
    • The Action class is part of the org.openqa.selenium.interactions package in Selenium.
  • It offers ways to simulate keyboard input, and mouse events including click and double click, and mouse hover.
  • The Action class provides a range of methods for performing these types of interactions, which can be chained together to create more complex actions.
  • The Action class can be particularly useful for testing web applications that use drag-and-drop functionality, hover-over effects, or other types of advanced user interactions.