Selenium is an extensively used framework for automation testing. Selenium 4 has gained a lot of popularity due to its updated architecture and new capabilities. To get a quick read on the features introduced into Selenium 4, refer here.
One of the new features of Selenium 4 is an improved action class in Selenium 4. Actions class in Selenium is used to imitate input actions from the mouse and keyboard on specific web elements like Left click, Right click, Double click, etc. Refer here for details on the actions class in Selenium 3.
The Actions class, which takes the role of the classes in the org.openqa.selenium.interactions package, has been updated for Selenium 4 and now includes new methods.
- Click
The new function click(WebElement) that was added to the Actions class takes the place of moveToElement(onElement).click() technique.
Syntax is Click(WebElement): used to click a web element, just like it did in earlier Selenium versions.
@Test
public void clickAction() {
driver.get("https://www.terrificminds.com/contact/");
Actions action = new Actions(driver);
WebElement GetInTouch = driver.findElement(By.xpath("//*[@id='get-button']"));
action.click(GetInTouch).build().perform();
- DoubleClick
In Selenium 4, the doubleClick() function for double clicking on a WebElement has been replaced with the doubleClick(WebElement) method.
- ContextClick
In Selenium 4, the contextClick(WebElement) method has taken the role of contextClick(), which was used to right-click on a WebElement in the previous version.
@Test
Actions action = new Actions(driver);
WebElement OpenDesign = driver.findElement(By.xpath("//a[@href="https://www.terrificminds.com/design/"][1]"));
action.contextClick(OpenDesign).build().perform();
- ClickAndHold
For clicking on a WebElement without performing the Release action , the method moveToElement(Element).clickAndHold() is used. In Selenium 4, it is replaced with clickAndHold(WebElement).
- Release
The release() method, which is used to release a pushed mouse button, was a component of the org.openqa.selenium.interactions. ButtonReleaseAction class in the previous Selenium release. The method is a component of the Actions class in Selenium 4, though.
@Test
Actions action = new Actions(driver);
WebElement Source = driver.findElement(By.xpath(of Source element));
WebElement Destination = driver.findElement(By.xpath(of Destination));
action.clickAndHold(Source).release(Destination).build().perform();
Conclusion
The actions class in Selenium is necessary in order to imitate any keyboard activities on the web application, like double-clicking, picking options from drop-down menus, right-clicking, left-clicking, etc. Selenium 4 has brought improvements in the action class which again improves the performance of the automation testing.