Wednesday 3 April 2013

Magic of Actions class with WebDriver

Most of us have been facing issues while automating elements like:

  1. Sliders
  2. Resizing an Element
  3. Drag & Drop 
  4. Hovering a mouse, specially in a case when dealing with mouse over menus.
This had been a complex task for any automation tool to handle such components. With WebDriver using the Actions class the same can be handled very easily and without applying any complex logics to it.

Such Elements like:

SLIDERS












Test sites:

Slider:  http://jqueryui.com/resources/demos/slider/default.html


Resizable:  http://jqueryui.com/resources/demos/resizable/default.html

Drag & Drop: http://jqueryui.com/resources/demos/droppable/default.html


Call the actions class and pass the driver object in the constructors.

Actions action = new Actions(driver);


Important Methods that will do the magic:

For Dragging & Dropping an element from source to target:

action.dragAndDrop(WebElement source, WebElement target)

For Sliding and Resizing an Element

action.dragAndDropBy(WebElement source, int xOffset, int yOffset)


Code:

Dragging & Dropping an Element:



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 testDragandDrop {


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


WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resources/demos/droppable/default.html");


WebElement draggable = driver.findElement(By.xpath("//*[@id='draggable']"));
WebElement droppable = driver.findElement(By.xpath("//*[@id='droppable']"));


Actions action = new Actions(driver);

action.dragAndDrop(draggable, droppable).perform();





}

}



Sliding an Element:



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 testSlider {

/**
* @param args
* @throws InterruptedException 
*/
public static void main(String[] args) throws InterruptedException {




WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resources/demos/slider/default.html");

WebElement slider = driver.findElement(By.xpath("//*[@id='slider']/a"));

Actions action = new Actions(driver);
Thread.sleep(3000);
action.dragAndDropBy(slider, 90, 0).perform();



}

}



Re-sizing an Element:



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 testResizable {


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

WebDriver driver = new FirefoxDriver();
driver.get("http://jqueryui.com/resources/demos/resizable/default.html");


WebElement resize = driver.findElement(By.xpath("//*[@id='resizable']/div[3]"));

Actions action = new Actions(driver);
action.dragAndDropBy(resize, 400, 200).perform();


}

}




Selenium training