Sunday 26 July 2015

Work with Actions

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

------------------
 

WebElement element = driver.findElement(By.linkText("Product Category"));

 Actions action = new Actions(driver);

 action.moveToElement(element).build().perform();
------------

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

No comments:

Post a Comment