Working with Action Interface in Selenium – User friendly Tech help

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.

n

n

n

n

n

n

n

n

Lets do some Action in Selenium

n

n
nExample:-

n

    n

  • To double-click a control we have DoubleClick(), method of Actions Class.
  • n

  • To Send keyboard actions, we have SendKeys() ,method.(Equivalent to WebElement.sendKey(…))
  • n

  • Clicking the mouse button that brings up the contextual menu.we can use “ContextClick()”, method.
  • n

n

Package:-
n
import org.openqa.selenium.interactions.Actions;
nimport org.openqa.selenium.interactions.Action;
n
nSyntax:-
n

n

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

n

n

n

n

n

n

n

n

Actions Class Methods

n

n
nExplanation:-
n
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.

n

What about multiple mouse and keyboard actions?
n
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.

n

Code:-
n

n

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

n

nScenarios:-
n
1.How to right click and open the application in new window.
n
Code:-

n

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

n

n
n
n2.How to fetch tool tip of a control in Selenium .
n
nCode:-

n

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

n

n3.How to send text in capital letters into a text-box?
n
nCode:-

n

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(); }}

n

n4.How we can drag and drop element in selenium?
n  
nCode:-

n

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"); }}

n

n
nNote:- we can also re-size the control, just need to change syntax a bit 
noAction.dragAndDropBy(resize, 400, 200).perform();
n
n5.How to refresh a application window using Action class?
n
nCode:-

n

 Actions oAction=new Actions(selenium);
oActions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
Was this article helpful?
YesNo

Similar Posts