Actions in Selenium

Double click on the element
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath(”

//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]”))).doubleClick().perform();

Mouse Hover on the element
// Instantiating Action Interface
Actions actions=new Actions(driver);
// howering on the dropdown
actions.moveToElement(driver.findElement(By.id(“id of the dropdown”))).perform();
// Clicking on one of the items in the list options
WebElement subLinkOption=driver.findElement(By.id(“id of the sub link”));
subLinkOption.click();

WebElement element = driver.findElement(By.linkText(“Product Category”));
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();

Scrolling
FirefoxProfile profile=new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver=new FirefoxDriver(profile);
driver.navigate(“http://jqueryui.com/draggable/);
Thread.sleep(6000L);
WebElement element=driver.findElement(By.xpath(“//div[@id=’draggable’]”));
Actions actn=new Actions(driver);
actn.dragAndDropBy(element, 50, 50).build().perform();

How to perform right click using WebDriver?
Ans- Use Actions class
Actions act = new Actions(driver); // where driver is WebDriver type
act.moveToElement(webElement).perform();
act.contextClick().perform();

How do perform drag and drop using WebDriver?
Ans- Use Action class
Actions act = new Actions(driver);
WebElement source = driver.findElement(By.xpath(? —–?)); //source ele which you want to drag
WebElement target = driver.findElement(By.xpath(? —–?)); //target where you want to drop
act.dragAndDrop(source,target).perform();

Ex :
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

public class webdriverdemo {

public static void main(String[] args) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
//Puts a Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Launch website
driver.navigate().to(“http://www.keenthemes.com/preview/metronic/templates/admin/ui_tree.html);
driver.manage().window().maximize();

WebElement From = driver.findElement(By.xpath(“.//*[@id=’j3_7′]/a”));
WebElement To = driver.findElement(By.xpath(“.//*[@id=’j3_1′]/a”));

Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(From).moveToElement(To).release(To).build();

dragAndDrop.perform();
driver.close();
}
}

In order to perform a ‘mouse hover’ action, we need to chain all of the actions that we want to achieve in one go. So move to the element that which has sub elements and click on the child item. It should the same way what we do normally to click on a sub menu item.
With the actions object you should first move the menu title, and then move to the sub menu item and click it.

Below is the sample code to perform Mouse hover action
Example 1:
Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText(“menulink”));
actions.moveToElement(mainMenu);

WebElement subMenu = driver.findElement(By.cssSelector(“subLinklocator”));
actions.moveToElement(subMenu);
actions.click().build().perform();

Example 2:
Actions action = new Actions(webdriver);
WebElement mainMenu = webdriver.findElement(By.linkText(“MainMenu”));
action.moveToElement(mainMenu).moveToElement(webdriver.findElement(By.xpath(“submenuxpath”))).click().build().perform();

There are cases where you may just want to mouse hover on particular element and check if the button state/color is changing after mouse hover.
Below is the example to perform mouse hover

WebElement searchBtn = driver.findElement(By.id(“searchbtn”));
Actions action = new Actions(driver);
action.moveToElement(searchBtn).perform();

In Webdriver, handling keyboard events and mouse events (including actions such as Drag and Drop or clicking multiple elements With Control key) are done using the advanced user interactions API . It contains Actions and Action classes which are needed when performing these events.
In order to perform action events, we need to use org.openqa.selenium.interactions.Actions class.
Here is the sample code to work with Action Class

// Configure the Action
Actions action = new Actions(driver);
// To click on the element
action.moveToElement(element).click().perform();

Note: We need to use perform() to execute the action.
Using Action API, keyboard interactions are simple to do with webdriver. In Advanced User Interactions API, interaction with element is possible either by clicking on element or sending a Keys using sendKeys()
To use mouse actions, we need to use current location of the element and then perform the action.

The following are the regularly used mouse and keyboard events :

Method :clickAndHold()
Purpose: Clicks without releasing the current mouse location

Method : contentClick()
Purpose: Performs a context-click at the current mouse location.

How to work with context menu by taking a simple example

Method: doubleClick()
Purpose: Performs a double click at the current mouse location

Method: dragAndDrop(source,target)
Parameters: Source and Target
Purpose: Performs click and hold at the location of the source element and moves to the location of the target element then releases the mouse.

Method : dragAndDropBy(source,x-offset,y-offset)
Parameters: Source, xOffset – horizontal move, y-Offset – vertical move Offset
Purpose: Performs click and hold at the location of the source element moves by a given off set, then releases the mouse.

Method: keyDown(modifier_key)
Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONROL)
Purpose: Performs a modifier key press, doesn’t release the modifier key. Subsequent interactions may assume it’s kept pressed

Method: keyUp(modifier_key)
Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONROL)
Purpose: Performs a key release.

Method: moveByOffset(x-offset, y-offset)
Parameters: X-Offset , Horizontal offset, a negative value means moving the mouse to left side.
Y-Offset, vertical offset, a negative value means moving the mouse to up.
Purpose: Moves the mouse position from its current position by the given offset.
Check for the example here Resizing a web element using movebyoffset

Method: moveToElement(toElement)
Parameters: toElement – Element to Move to
Purpose: It moves the Mouse to the middle of the element.

Method: release()
Purpose: It releases the left mouse button at the current mouse location.

Method: sendKeys(onElement, charSequence)
Parameters: onElement, which will receive the keyStrokes, usually text field.
charsequence- any string value representing the sequence of keyStrokes to be sent.
Purpose: It sends a series of keyStrokes onto the element

Its not really possible to perform a ‘mouse hover’ action, instead you need to chain all of the actions that you want to achieve in one go. So move to the element that reveals the others, then during the same chain, move to the now revealed element and click on it.
When using Action Chains you have to remember to ‘do it like a user would’.

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath(“html/body/div[13]/ul/li[4]/a”));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath(“/expression-here”))).click().build().perform();

Scroll down the Window
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript(“window.scrollBy(0,4500)”, “”); //scroll down, value 4500 you can change as per your req
jsx.executeScript(“window.scrollBy(450,0)”, “”); //scroll up

Ex:
public class ScrollDown {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“http://www.flipkart.com/womens-clothing/pr?sid=2oq,c1r&otracker=hp_nmenu_sub_women_1_View%20all);
driver.manage().window().maximize();
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript(“window.scrollBy(0,4500)”, “”); //scroll down
Thread.sleep(3000);
jsx.executeScript(“window.scrollBy(450,0)”, “”); //scroll up
}
}

Select Multiple

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

public class webdriverdemo {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.navigate().to(“http://demos.devexpress.com/aspxeditorsdemos/ListEditors/MultiSelect.aspx);

//driver.manage().window().maximize();
driver.findElement(By.id(“ContentHolder_lbSelectionMode_I”)).click();
driver.findElement(By.id(“ContentHolder_lbSelectionMode_DDD_L_LBI1T0”)).click();
Thread.sleep(5000);

// Perform Multiple Select
Actions builder = new Actions(driver);
WebElement select = driver.findElement(By.id(“ContentHolder_lbFeatures_LBT”));
List options = select.findElements(By.tagName(“td”));

System.out.println(options.size());
Action multipleSelect = builder.keyDown(Keys.CONTROL).click(options.get(2)).click(options.get(4)).click(options.get(6)).build();

multipleSelect.perform();
driver.close();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s