We can easily create a test script to implement drag and drop operation using the Action class
Example of droppable
package basicexample;
import java.util.concurrent.TimeUnit;
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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DragandDropExample {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","d:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
String URL = "https://demoqa.com/droppable/";
driver.get(URL);
// It is always advisable to Maximize the window before performing DragNDrop action
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
//Actions class method to drag and drop
Actions builder = new Actions(driver);
WebElement from = driver.findElement(By.id("draggable"));
WebElement to = driver.findElement(By.id("droppable"));
//Perform drag and drop
builder.dragAndDrop(from, to).perform();
//verify text changed in to 'Drop here' box
String textTo = to.getText();
if(textTo.equals("Dropped!")) {
System.out.println("PASS: Source is dropped to target as expected");
}else {
System.out.println("FAIL: Source couldn't be dropped to target as expected");
}
// driver.close();
}
}
Example of draggable:-
package basicexample;
import java.util.concurrent.TimeUnit;
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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DragandDropExample {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","d:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
String URL = "https://jqueryui.com/draggable/";
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
//Actions class method to drag and drop
Actions builder = new Actions(driver);
WebElement from = driver.findElement(By.id("draggable"));
//Perform dragAndDropBy
builder.dragAndDropBy(from, 100,100).perform();
System.out.println("Dropped");
driver.close();
}
}
Draggable Example in Selenium:-
package scs;
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 DraggableExampleNew {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.guru99.com/test/drag_drop.html");
driver.manage().window().maximize();
WebElement From=driver.findElement(By.xpath("//*[@id=\"credit4\"]/a"));
Actions act=new Actions(driver);
Thread.sleep(1000);
act.dragAndDropBy(From,160, 50).build().perform();
}
}
POST Answer of Questions and ASK to Doubt