Understanding Actions API in Selenium WebDriver

In this blogpost, we are going to learn about one of the most important class and its methods, used to perform composite/ complex mouse and keyboard actions on any of the web elements on the webpage.

We can perform complex actions like mouse hover, drag and drop, double click, context click etc. using the Actions class’ methods.

So, Actions is a class provided in WebDriver API.

How to get an instance of Actions class?

We can create an instance of Actions class simply, by passing current webDriver instance.

WebDriver driver  = new FirefoxDriver();

Actions action  = new Action(driver);

Now, using the action instance, we can call any of the methods provided in Actions class.

How to see all the supported methods by Actions class?

In order to see all the supported methods by Actions class, press control + space after,

action.

Actions1

Actions2

Actions3

So, we can use any of the methods as per our need. Subsequently, we will have a look some of these important methods.

Some important  methods of Actions class :

build command :

Usage : Used to generate a composite action, containing all actions so far, ready to be performed

Syntax : Action build();

Return type : Action interface instance

Parameters : Nothing

Example :

WebDriver driver = new FirefoxDriver();
driver.get(“https://seleniumatfingertips.wordpress.com”);
driver.manage().window().maximize();

WebElement elem = driver.findElement(By.tagName(“h1”));
Actions action = new Actions(driver);
Action builder = action.moveToElement(elem).click().build();

The build() method creates composite action combining all the previous action methods called, in this example, it is moveToElement(elem) and click() method.

action.action1().action2()…..actionN().build().perform();

perform command :

Usage : Used to perform the composite action (set of actions).

Syntax : void perform();

Return type : void

Parameters : Nothing

Example :

WebDriver driver = new FirefoxDriver();
driver.get(“https://seleniumatfingertips.wordpress.com”);
driver.manage().window().maximize();

WebElement elem = driver.findElement(By.tagName(“h1”));
Actions action = new Actions(driver);
action.moveToElement(elem).click().perform();

The perform() method internally calls the build() method, which returns Action interface reference variable, and then calls perform() method which is defined in Action interface.

So, either we can call build() method and then perform() method, or we can call perform() method directly without calling build() method.

It is very important to understand the internal behaviour of above general syntax of using Actions class’ build() and perform() methods.

So, first of all, series of actions supported by Actions class can be called using action class instance (action1(), action2()…… actionN()), which returns an instance of Actions class.

Secondly, build() method of Actions class is called on returned Actions class instance, which builds a composite action, and returns Action interface’s reference variable.

And at last, perform() method of Action interface is called on returned reference variable of Action interface, which performs a series of actions built by build() method.

doubleClick command :

Usage : Used to double click on a particular web element on the web page.

Syntax : Actions doubleClick(WebElement onElement)

Return type : Actions class instance

Parameters : WebElement

Example :

WebDriver driver = new FirefoxDriver();
driver.get(“https://seleniumatfingertips.wordpress.com”);
driver.manage().window().maximize();

WebElement elem = driver.findElement(By.tagName(“h1”));
Actions action = new Actions(driver);
action.doubleClick(elem).build().perform();

contextClick command :

Usage : Used to perform right click on a particular web element on the web page.

Syntax : Actions contextClick(WebElement onElement)

Return type : Actions class instance

Parameters : WebElement

Example :

WebDriver driver = new FirefoxDriver();
driver.get(“https://seleniumatfingertips.wordpress.com”);
driver.manage().window().maximize();

WebElement elem = driver.findElement(By.tagName(“h1”));
Actions action = new Actions(driver);
action.contextClick(elem).perform();

dragAndDrop command :

Usage : Used to perform drag and drop action on the web page.

Syntax : Actions dragAndDrop(WebElement source, WebElement target)

Return type : Actions class instance

Parameters : Source and Target WebElement

Example :

WebDriver driver = new FirefoxDriver();
driver.get(“https://seleniumatfingertips.wordpress.com”);
driver.manage().window().maximize();

WebElement elemSource = driver.findElement(By.tagName(“h1”));

WebElement elemTarget = driver.findElement(By.tagName(“h2”));
Actions action = new Actions(driver);
action.contextClick(elem).perform();

action.dragAndDrop(elemSource, elemTarget);

The method performs click-and-hold function at the specified source element, moves to the control to the location of target element and then releases the mouse.

There are numerous methods available in Actions class, which turns out to be very useful while performing composite Mouse and Keyboard actions.

So, that’s all from this blogpost.

Hope it helps you a lot!!

Cheers 🙂

 

 

Advertisements

#actions-in-webdriver, #build, #composite-actions-in-webdriver, #double-click-in-webdriver, #drag-and-drop-selenium-webdriver, #perform