Skip to content

Instantly share code, notes, and snippets.

@bbonamin
Created July 25, 2012 14:51
Show Gist options
  • Select an option

  • Save bbonamin/3176573 to your computer and use it in GitHub Desktop.

Select an option

Save bbonamin/3176573 to your computer and use it in GitHub Desktop.
Java selenium drag and drop
bootons = driver.findElements(By.cssSelector("(removed)"));
for (int i = 0; i < bootons.size(); i++) {
WebElement booton = bootons.get(i);
List < WebElement > targets = driver.findElements("(removed)"));
for (int j = 0; j < targets.size(); j++) {
WebElement target = targets.get(j);
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(booton).moveToElement(target).release().build();
dragAndDrop.perform();
}
}
=== === === === === === === === = Example 2: === === === === === === === === =
import java.util.List;
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;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class ActionExample {
private static WebDriver driver;@BeforeClass public void setUp() {
driver = new FirefoxDriver();
}@AfterClass public void tearDown() {
driver.close();
driver.quit();
}@Test public void draggable() {
driver.get("http://jqueryui.com/demos/draggable/");
WebElement draggable = driver.findElement(By.id("draggable"));
new Actions(driver).dragAndDropBy(draggable, 120, 120).build().perform();
}@Test public void droppable() {
driver.get("http://jqueryui.com/demos/droppable/");
WebElement draggable = driver.findElement(By.id("draggable"));
WebElement droppable = driver.findElement(By.id("droppable"));
new Actions(driver).dragAndDrop(draggable, droppable).build().perform();
}@Test public void selectMultiple() throws InterruptedException {
driver.get("http://jqueryui.com/demos/selectable/");
List < WebElement > listItems = driver.findElements(By.cssSelector("ol#selectable *"));
Actions builder = new Actions(driver);
builder.clickAndHold(listItems.get(1)).clickAndHold(listItems.get(2)).click();
Action selectMultiple = builder.build();
selectMultiple.perform();
}@Test public void sliding() {
driver.get("http://jqueryui.com/demos/slider/");
WebElement draggable = driver.findElement(By.className("ui-slider-handle"));
new Actions(driver).dragAndDropBy(draggable, 120, 0).build().perform();
}
} === === === === === === === === === === == Example 3: === === === === === === === === === === ==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment