Action class performs operations on keyboard and mouse events. Like refresh, double click, mouse over etc. WebElement cannot perform those operations.
Action class of selenium exists in the package ‘org.openqa.selenium.interactions’. Actions given in below table are most commonly used functions of Action class.
Method Name | Description |
---|---|
addAction | Adds an action to current list of actions to be performed. |
build | Builds the sequence of actions. |
click() | Clicks the mouse at the last known mouse coordinates. |
click(IWebElement) | Clicks the mouse on the specified element. |
clickAndHold() | Clicks and holds the mouse button at the last known mouse coordinates. |
clickAndHold(IWebElement) | Clicks and holds the mouse button down on the specified element. |
contextClick() | Right-clicks the mouse at the last known mouse coordinates. |
contextClick(IWebElement) | Right-clicks the mouse on the specified element. |
doubleClick() | Double-clicks the mouse at the last known mouse coordinates. |
doubleClick(IWebElement) | Double-clicks the mouse on the specified element. |
dragAndDrop | Performs a drag-and-drop operation from one element to another. |
dragAndDropToOffset | Performs a drag-and-drop operation on one element to a specified offset. |
equals | Determines whether the specified Object is equal to the current Object.(Inherited from Object.) |
finalize | Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.(Inherited from Object.) |
getHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
getLocatableFromElement | Gets the ILocatable instance of the specified IWebElement. Gets the Type of the current instance. |
getType | (Inherited from Object.) |
keyDown(String) | Sends a modifier key down message to the browser. |
keyDown(IWebElement, String) | Sends a modifier key down message to the specified element in the browser. |
keyUp(String) | Sends a modifier key up message to the browser. |
keyUp(IWebElement, String) | Sends a modifier up down message to the specified element in the browser. |
memberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
moveByOffset | Moves the mouse to the specified offset of the last known mouse coordinates. |
moveToElement(IWebElement) | Moves the mouse to the specified element. |
moveToElement(WebElement, Int32, Int32) | Moves the mouse to the specified offset of the top-left corner of the specified element. |
perform | Performs the currently built action. |
release() | Releases the mouse button at the last known mouse coordinates. |
release(IWebElement) | Releases the mouse button on the specified element. |
sendKeys(String) | Sends a sequence of keystrokes to the browser. |
sendKeys (IWebElement,String) | Sends a sequence of keystrokes to the specified element in the browser. |
to String | Returns a String that represents the current Object. (Inherited from Object.) |
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
Actions builder = new Actions(driver);
Action mouseOverHome = builder.moveToElement(link_Home).build();
In this case, we are going to use the moveToElement() method because we are simply going to mouse-over the "Home" link. The build () method is always the final method used so that all the listed actions will be compiled into a single step.
mouseOverHome.perform();
You can build a series of multiple actions using the Action and Actions classes. Just remember to close the series with the build () method.
package com.javabykiran.automation;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class ActionsEx {
public static void main(String[] args) {
// opens a Firefox browser
WebDriver driver = new FirefoxDriver();
// opening of url driver.get("http://javabykiran.com/javabykiranfiles/");
Actions builder = new Actions(driver);
WebElement userName = driver.findElement(By.xpath(".//*[@ id='login_form_user']"));
Action seriesOfActions = builder.moveToElement(userName).click().keyDown(userName, Keys.SHIFT).
//press shift//for uppercase sendKeys(userName, "javabykiran"). // writes text doubleClick(userName).
// highlights test contextClick().// brings up menu
build();
seriesOfActions.perform();
}
}