Working with Action Interface in Selenium

To perform Advanced User Interactions, like drag and drop, keyboard events; selenium came up with Action and Actions interface under a  comprehensive API, named as Advanced User Interaction which facilitate user actions to be performed on a application. Thus users can use this API to simulate usage of keyboard or mouse events.

Learn Action class in Selenium
Lets do some Action in Selenium



Example:-

  • To double-click a control we have DoubleClick(), method of Actions Class.
  • To Send keyboard actions, we have SendKeys() ,method.(Equivalent to WebElement.sendKey(…))
  • Clicking the mouse button that brings up the contextual menu.we can use “ContextClick()”, method.

Package:-

import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

Syntax:-

 Actions oAction=new Actions(selenium);
oAction.contextClick(element).perform();

  

Methods of Action class selenium
Actions Class Methods



Explanation:-

We need to import above packages to access the action and actions class methods.Create the actions class object and access the range of available methods.

What about multiple mouse and keyboard actions?

In above case we simply performed one action on the element, incase we want to perform sequence of events on the same element, we can achieve same by using build method.

Code:-

   Actions oAction=new Actions(selenium);
Actions moreActions = oAction
.moveToElement(element)
.click()
.keyDown(element,Keys.SHIFT)
.sendKeys(element,"selenium");

Action enterInCaps= moreActions.build();
enterInCaps.perform();


Scenarios:-

1.How to right click and open the application in new window.

Code:-

        Actions oAction=new Actions(selenium);
oAction.contextClick(element).perform();
oAction.sendKeys("w").perform();




2.How to fetch tool tip of a control in Selenium .

Code:-

 Actions oAction=new Actions(selenium);
oAction.clickAndHold(element).perform();


3.How to send text in capital letters into a text-box?

Code:-

import java.util.concurrent.TimeUnit;
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 ActionClass {

public static void main(String[] args) {
WebDriver selenium = new FirefoxDriver();
System.out.println("Launching Browser");
//Opening the URL
selenium.get("http://www.uftHelp.com");
//Implicit wait for the browser to launch
selenium.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Google Search Text-box
WebElement element =selenium.findElement(By.id("gsc-i-id1"));
//Creating action class object
Actions oAction=new Actions(selenium);
//Creating action collection to perform numerous methods on element
Actions moreActions = oAction
.moveToElement(element)
.click()
.keyDown(element,Keys.SHIFT) //for caps
.sendKeys(element,"selenium");

Action enterInCaps= moreActions.build();
enterInCaps.perform();
System.out.println("Text is entered in Captial letters");
//Closing the browser
//selenium.quit();
}

}


4.How we can drag and drop element in selenium?
  
Code:-

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class DragAndDrop {

public static void main(String[] args) {

WebDriver selenium = new FirefoxDriver();
System.out.println("Launching Browser");
//Opening the URL
selenium.get("http://jqueryui.com/resources/demos/droppable/default.html");
//Implicit wait for the browser to launch
selenium.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Identifying the elements to perform action
WebElement draggable = selenium.findElement(By.xpath("//*[@id='draggable']"));
WebElement droppable = selenium.findElement(By.xpath("//*[@id='droppable']"));
Actions oAction = new Actions(selenium);
//Performing Drag and Drop operation
oAction.dragAndDrop(draggable, droppable).perform();
System.out.println("Successfully completed the Drag-Drop operation");
}

}



Note:- we can also re-size the control, just need to change syntax a bit 
oAction.dragAndDropBy(resize, 400, 200).perform();

5.How to refresh a application window using Action class?

Code:-

 Actions oAction=new Actions(selenium);
oActions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();


2 thoughts on “Working with Action Interface in Selenium

  1. I am using geckho driver(testing throguh graphene) , but same code is working in chrome but not working in firefox.

    @Drone
    private WebDriver browser

    final Actions builder = new Actions(browser);
    builder.keyDown(Keys.CONTROL)
    .click(alphaFaultyNodeItem)
    .click(betaFaultyNodeItem)
    .keyUp(Keys.CONTROL);

    final Action selectMultiple = builder.build();
    selectMultiple.perform();

    firefox version: 53.0.3
    Geckho driver : v0.16.1
    selenium : 3.3.1

    Tried with selenium : 3.4.0 as well

Leave a Reply

Your email address will not be published. Required fields are marked *