What is Action Class in Selenium

In this article we learn the what is action class in selenium, syntax of action class and most commonly used methods of action class in selenium.

What is Action Class in Selenium WebDriver: –

What is Action in Selenium?

Action in Selenium is an interface that provides us two methods: perform () and build (). These two methods or commands of action interface are implemented by the action class.

We can perform mouse based and keyboard-based operations in action class.

1) build () Method: –

The build () method of action interface generates a composite action that contains all the actions gathered which are ready to be performed.

2) perform () Method: –

This method is used to perform a sequence of actions without calling build () first.

Syntax of Action Class in selenium-

Actions act = new Actions (driver);

Actions is an ordinary class, its object is created by using the new keyword. After creating object, we will have to pass WebDriver object reference as a parameter to its constructor. We can access all the methods provided by the Actions class.

Mouse based Methods of Action Class in Selenium: –

1) click ():

Clicks at the current mouse location.

2) doubleClick ():

The doubleClick () method is used to perform a double-clicking at the current location of the mouse cursor.

3) doubleClick (WebElement element):

This method is used to perform a double-clicking in the middle of the given element.

4) contextClick ():

The contextClick () method is used to perform a right-click at the current mouse location.

5) contextClick (WebElement element):

This method is an overloaded version of contextClick () method that is used to perform a right-click on the given web element. First, it moves the cursor to the location of web element and then performs right-click on the element.

6) moveToElement (WebElement):

The moveToElement () method is used to move the mouse cursor to the center of a specified element on the web page. The web element is also scrolled into view.

7) clickAndHold ():

The clickAndHold () method is used to left-clicking on an element and holds it without releasing the left button of the mouse (i.e. without releasing at the current mouse location).

8) release ():

The release () method is used to release or to drop an element from the mouse. It can release the left mouse button on a web element.

9) dragAndDrop (WebElement, WebElement):

The dragAndDrop () method is used to drag an element on to the location of another target element and drop it. We need to pass source element and target element to dragAndDrop () method of actions class where the source element will be dragged and dropped.

10) dragAndDropBy (WebElement, int, int):

The dragAndDropBy () method is used to drag a web element on to the location of given offset and drop it.

Keyboard based Methods of Action Class in Selenium: –

1) keyDown ():

The keyDown () method is used to perform the action of pressing and holding a key. The keys can be Shift, Ctrl, and Alt keys.

2) keyUp ():

The keyUp () method is used to release a key that has been already pressed using keyDown () method.

3) sendKeys (CharSequence):

The sendKeys () method is used to type in alphanumeric and special character keys into the WebElement such as textarea, textbox, and so on.

Difference between Action and Actions class in Selenium

1. Action in selenium is an interface whereas, actions is a class in selenium.

2. Action provides two methods such as build () and perform () whereas, actions class provides various methods to perform complex mouse actions and keyboard shortcuts.

3. Actions class extends object class and implements an action interface.

Other Popular Articles

How to Handle Alert In Selenium WebDriver?

Leave a Comment