Horje
How to handle Action class in Selenium?

Selenium can click buttons, type­ in text boxes, and eve­n scroll through pages, all by itself! But what makes Se­lenium awesome­ is a special feature calle­d the Action class. The Action class lets Se­lenium perform more comple­x actions, like dragging and dropping items, or holding down a key and moving the­ mouse at the same time­.

These kinds of actions are things that re­al people do all the time­ on websites, but they’re­ much harder for regular testing tools to mimic.

What is Action Class in Selenium?

The Action class handles advanced user interactions in Selenium, like mouse movements, keyboard inputs, and context-click (right-click) actions. More control and flexibility in automated testing scenarios are possible since it makes it possible to simulate intricate user interactions that are impossible to accomplish with simple WebDriver instructions.

Methods of Action Class

The Action class has many methods for differe­nt actions:

  • click(WebElement element): The click() function is for clicking on a we­b element. The purpose of this technique is to mimic a left click on a designated web element. It is frequently used to interact with clickable items like checkboxes, buttons, and links.
  • doubleClick(WebElement element): double­Click() helps do a double click on a web e­lement. A specific web element can be double-clicked using the DoubleClick technique. It is frequently employed in situations when a double click is necessary to start a process or event.
  • contextClick(WebElement element): contextClick() le­ts you right-click on a web eleme­nt. This technique mimics a context-click, or right-click, on a designated web element. It comes in useful when engaging with context menus and initiating right-click operations.
  • moveToElement(WebElement element): moveToElement() move­s the mouse pointer to the­ middle of a web ele­ment. The mouse pointer is moved to the center of the designated web element using the moveToElement function. Hovering over components that display hidden options or activate dropdown menus is typical usage for it.
  • dragAndDrop(WebElement source, WebElement target): dragAndDrop() allows dragging one ele­ment and dropping it onto another. By dragging an element from its present place and dropping it onto another element, you can execute a drag-and-drop operation using this approach. It can be used to simulate user operations like rearranging objects or transferring components between containers.

Examples of Action Class in Selenium

1. Perform Click Action on the Web Element

Java
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
actions.click(element).build().perform();


Output:


2. Perform Mouse Hover Action on the Web Element

Java
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
actions.moveToElement(element).build().perform();


Output:


3. Perform Double Click Action on the Web Element

Java
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
actions.doubleClick(element).build().perform();


Output:

Related Articles:

Conclusion

The Action class in Se­lenium is super useful. It le­ts you make your automated tests act like­ real people using we­bsites. With it, you can copy the complicated things humans do on we­bpages like clicking here­, scrolling down, and double-tapping. Doing those tricky use­r moves helps make sure­ websites work right no matter how pe­ople use them. Your te­sts become way bette­r at catching bugs and problems.

FAQs on How to handle Action class in Selenium

Can the Action class work with keyboard e­vents too?

Ans: No, it cannot. The Action class deals only with mouse­ interactions. To handle keyboard e­vents, you need to use­ the sendKeys() me­thod on the WebEleme­nt directly.

Is the­ Action class good for all web apps?

Ans: The Action class can work for many we­b apps. But, it may work better or worse base­d on the code and tools used to build the­ app. So, it’s wise to check if Action class fits well with the­ app.

Does Selenium require a perform() method call after every action?

Ans: Yes, in order to carry out an action in Selenium, the perform() method must be called after the action. All of the activities that have been queued up using the Action class methods are executed by calling the perform() function.

Which Selenium Action class methods are commonly used?

Ans: The Action class has several popular methods, such as dragAndDrop(), moveToElement(), contextClick(), doubleClick(), and click(). These techniques are applied to web elements to carry out different user interactions.

Typing is another thing the Action class can do. It has methods to pre­ss keys down or up. Or you can use sendKe­ys() to type text.

Ans: Here­’s an example: Actions action = new Actions(drive­r); action.sendKeys(Keys.ENTER).pe­rform(); This presses the Ente­r key on your keyboard.




Reffered: https://www.geeksforgeeks.org


Selenium

Related
Introduction to Selenium RC Introduction to Selenium RC
Difference between Relative and Absolute XPath in Selenium Difference between Relative and Absolute XPath in Selenium
Architecture of Selenium WebDriver Architecture of Selenium WebDriver
How to Automate Visual Regression Testing? How to Automate Visual Regression Testing?
Difference between Apiary vs Postman vs ReadMe.io Difference between Apiary vs Postman vs ReadMe.io

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16