Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Monday, August 29, 2016

How to drag and drop an element to location specified by X, Y coordinate using selenium

Drag and drop is now common functionality across many web applications for interactive user interface and easy upload. 

To automate drag and drop in your automation test script, selenium provides a class called Actions for interactive actions.

Scenario:
1. Launch https://jqueryui.com/draggable/
2. Drag and release the element an X=250 and Y= 10 Pixels.



To automate the above scenario using Selenium Webdriver , we would need to use dragAndHold(WebElement element) function of selenium library.

Code:



package seleniumInteractive;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class DragAndDrop {

	static {
		System.setProperty("webdriver.chrome.driver", "Chrome driver location");
	}

	public static void main(String[] args) {

		WebDriver driver = new ChromeDriver();
		driver.get("https://jqueryui.com/draggable/");
		driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
		WebElement element = driver.findElement(By.id("draggable"));
		Actions action = new Actions(driver);
		action.clickAndHold(element).moveByOffset(250, 10).build().perform();
	}

}

POC videos:








Please follow our other blog Tester in you for more updates and tutorials of Selenium.

No comments: