How to Drag and Drop Using Action Class in Java Selenium?

In this post, we will learn about another feature of the action class: drag and drop in selenium. Also, we will learn how to implement this in Selenium using action class.

This feature is mostly used in Windows applications, but nowadays, you can also get the same feature in web applications. For example, you can use Gmail when composing a mail at that time; when you click on the attachment, you can upload a file and drop it in the upload window.

When you are doing automation to perform this drag-and-drop operation using selenium, no direct method is available. To perform this, we can use the Actions class, which has various methods to do such operations.

In the Action class, there are some methods for drag and drop:

  • dragAndDrop(elementsource, elementtarget)
  • dragAndDropBy(elementsource, int xOffset, int yOffset)

Drag and Drop Action in Selenium

dragAndDrop(elementsource, elementtarget): When we use this method, this method clicks and holds on to the source element and moves the source element to the target location element; after that, it releases the mouse click.

Let’s see what happens internally when invoking the perform() method above

  • Click And Hold Action: The dragAndDrop() method first performs click-and-hold at the location of the source element.
  • Move Mouse Action: Then, the source element gets moved to the location of the target element.
  • Button Release Action: Finally, it releases the mouse
  • Build: The build() method generates a composite action containing all actions. But if you observe, we have not invoked it in our above command. The build is executed in the perform method internally.
  • Perform: The perform() method performs the actions we have specified. But before that, it internally invokes the build() method first. After the build, the action is performed.

Find below the steps of the scenario to be automated:

  • Launch the web browser and launch our practice page https://demoqa.com/droppable/
  • Find the required source element in our sample, i.e., the ‘Drag me to my target’ object.
  • Find the required target element in our sample, i.e., the ‘Drop Here’ object.
  • Now Drag and Drop the ‘Drag me to my target’ object to the ‘Drop Here’ object.
  • Verify the message displayed on ‘Drop Here’ to verify that the source element is dropped at the target element.
  • Close the browser to end the program
package com.toolsqa.tutorials.actions;

import java.util.concurrent.TimeUnit;

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

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

//Note: Following statement is required since Selenium 3.0,
//optional till Selenium 2.0
//Set system properties for geckodriver
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\Toolsqa\\lib\\geckodriver.exe");

WebDriver driver = new FirefoxDriver();

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

}

}

DragAndDropBy Used In Selenium

This method also works like the dragAndDrop method, but this dragAndDropBy method works differently. The dragAndDrop technique moves the source element to a specified location. Still, by using dragAndDropBy, we can move the source element to a specific coordinate by giving the X and Y coordinates. Where X represents horizontal movement, and Y represents vertical movement.

Scenario:

  • Launch the web browser and launch our practice page https://demoqa.com/droppable/
  • Find the required source element in our sample, i.e., the ‘Drag me to my target’ object.
  • Calculate the required xOffset and yOffset to drag the source element in the horizontal and vertical direction, respectively [For this, calculate xOffset by getting the difference between the x location of the source and target element.
  • Similarly, get yOffset by getting the difference between the y location of the source and target element]
  • Now Drag and Drop the ‘Drag me to my target’ object to the ‘Drop Here’ object.
  • Verify the message displayed on ‘Drop Here’ to verify that the source element is dropped at the target element.
  • Close the browser to end the program.
import java.util.concurrent.TimeUnit;

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

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

//Note: Following statement is required since Selenium 3.0,
//optional till Selenium 2.0
//Set system properties for geckodriver
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\Toolsqa\\lib\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

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

//Here, getting x and y offset to drop source object on target object location
//First, get x and y offset for from object
int xOffset1 = from.getLocation().getX();

int yOffset1 = from.getLocation().getY();

System.out.println("xOffset1--->"+xOffset1+" yOffset1--->"+yOffset1);

//Secondly, get x and y offset for to object
int xOffset = to.getLocation().getX();

int yOffset = to.getLocation().getY();

System.out.println("xOffset--->"+xOffset+" yOffset--->"+yOffset);

//Find the xOffset and yOffset difference to find x and y offset needed in which from object required to dragged and dropped
xOffset =(xOffset-xOffset1)+10;
yOffset=(yOffset-yOffset1)+20;

//Perform dragAndDropBy
builder.dragAndDropBy(from, xOffset,yOffset).perform();

//verify text changed in to 'Drop here' box
//Get text value of 'to' element
String textTo = to.getText();

if(textTo.equals("Dropped!")) {
System.out.println("PASS: Source is dropped at location as expected");
}else {
System.out.println("FAIL: Source couldn't be dropped at location as expected");
}

driver.close();

}
}

Drag Drop Using Action Class: How do you drag drop using an action class in Java Selenium Example Program?

package com.selenium.actions;
import java.util.concurrent.TimeUnit;
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;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Action_DragNDrop 
{
   WebDriver driver=new FirefoxDriver();
   Actions act=new Actions(driver);
   @BeforeTest
   public void openwebsite()
   {
      driver.manage().window().maximize();
      driver.get("http://jqueryui.com/resources/demos/droppable/default.html");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
   }
   @Test
   public void operation() throws InterruptedException
   {
      Actions act=new Actions(driver);
      WebElement Source=driver.findElement(By.xpath(".//*[@id='draggable']"));
      WebElement Destination=driver.findElement(By.xpath(".//*[@id='droppable']"));
      act.dragAndDrop(Source, Destination).build().perform();	
   }
   @AfterTest
   public void close() throws InterruptedException
   {
      Thread.sleep(5000);
      driver.close();
   }
}

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment