×

Drag and Drop Event Handling

Drag and Drop Event Handling:

In this tutorial, we will learn how to handle the drag and drop event in selenium WebDriver using the action class.

Before going further in this tutorial first, we will understand what drag and drop is?

Drag and Drop:

This method is used to drag the element from the source and drop it to the target.

Or

Drag and drop are used to move (drag) the web element, text and, an image to another part /section of the web page (drop) with the help of the mouse.

In selenium WebDriver, we will use the drag and drop event with the help of action class.

drag and drop event handling

Let us take one example to give you a better understanding of drag and drop handling, and we created a sample test script which is as follows;

For our testing purpose, we are using the demo.telrik.com website to perform all the actions for drop and drag handling in selenium WebDriver.

In this test case, we will automate the following test scenarios:

Steps Action Method Used Input Excepted Result
1. Open the Google Chrome Browser. System.setProperty()   The Google Chrome browser must be opened.
2. Navigate to the demo.telrik.com. get() https://demos.telerik.com/kendo-ui/dragdrop/index The home page window must be displayed.
3. Identify the drag and drop frame window to perform all the actions.     The frame window should be identified.
3. Identify the source element (drag) using the id locator.     The source element should be identified for dragging.
4. Identify the target element (drop) using the id locator.     The target element should be identified for dropping.
5. Create an action class object.     The action class object should be created.
6. Click and hold the element and release it. clickAndHold(),moveToelement(),release()   Using these methods, the element should be drag from the source and drop it to the target element.
7. Quit the Browser. quit()   The browser should beterminated.

Open the Eclipse IDE and the existing test suite new_test, which we have created in WebDriver Installation section of the WebDriver tutorial.

Then, right-click on the src folder and create a new Class File from New Class.

drag and drop event handling 1
  • Give the Class name as Drag_and_drop and click on the Finish button.
drag and drop event handling 2

Step1:

To launch the Google Chrome browser, we need to download the ChromeDriver.exe file and set the system property to the path of ChromeDriver.exe file.

Here is the code for setting the system property for the google chrome:

//set the system property of Google Chrome
 System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");                                                                                            

To initialize the Chrome driver using ChromeDriver class:

// create driver object for CHROME browser
WebDriver driver=new ChromeDriver(); 

Step2:

Now, we will get to the desired URL:

//navigate to the home page of telrik.com.
driver.get("https://demos.telerik.com/kendo-ui/dragdrop/index"); 

Step3:

First, we have to find the drag-drop frame window, and then right-click on the inspect Element (Q).

drag and drop event handling 3

The developer tool window will be launched, with all the specific codes used in the development of the given link.

drag and drop event handling 4

Step4:

And after that, right-click on the source (drag) element and select inspect Element (Q).

drag and drop event handling 5

After that, the developer tool window is opened, then copy the value of its id attribute, i.e.,” draggable.”

drag and drop event handling 6

Here the sample code,

// //identify the drag and drop frame window to perform all the action
driver.findElement(By.xpath("//div[@class='demo-section k-content']"));
 //to get source locator
WebElement srcwb=driver.findElement(By.id("draggable"));
System.out.println("the source element is ready to drag"); 

Step5:

Now, we are moving our next steps, where we will click on the target (drop) element, and select the Inspect element field.

drag and drop event handling 7

The developer tool window will be launched, with all the specific codes used in the development of thegiven link.

drag and drop event handling 8

Copy the value of its id attribute, i.e.,”droptarget.”

Here the sample code,

//to get target locator
WebElement dstwb=driver.findElement(By.id("droptarget"));
System.out.println("the traget element is ready to drop"); 

Step6:

In the next step of our sample test script, where we are creating the action class object.

//create an object to the action class
 Actions act= new Actions(driver); 

Step7:

After that, we will use the action class method to drag the source element and drop it the target element.         

//click and hold, and release operation
act.clickAndHold(srcwb).moveToElement(dstwb).release().build().perform(); 
Thread.sleep(3000);
//or we can use this below method
act.dragAndDrop(srcwb, dstwb).build().perform();
 Thread.sleep(3000); 

Step8:                                                                                 

Finally, we terminate the process and quit the browser.

//quit the browser
driver.quit();  

After completing all the steps, our final test script will look like this:

package testpackage;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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.WebDriverWait;
public class Drag_and_drop {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//create driver class object
WebDriver driver = new ChromeDriver();
//maximize the browser window
driver.manage().window().maximize(); 
//to delete the all cookies
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
//navigate to the url
driver.get("https://demos.telerik.com/kendo-ui/dragdrop/index");
//using explicitly wait
WebDriverWait wait = new WebDriverWait(driver, 5);
//identify the drag and drop frame window to perform all the action
driver.findElement(By.xpath("//div[@class='demo-section k-content']"));
//to get source locator
WebElement srcwb=driver.findElement(By.id("draggable"));
System.out.println("the source element is ready to drag");
//to get target locator
 WebElement dstwb=driver.findElement(By.id("droptarget"));
System.out.println("the target element is ready to drop"); 
//create an object to the action class
Actions act=new Actions(driver);
//click and hold, and release operation
act.clickAndHold(srcwb).moveToElement(dstwb).release().build().perform();
Thread.sleep(3000);
System.out.print("the source element is dropped to the target successfully");
//or we can use this below method
//act.dragAndDrop(srcwb, dstwb).build().perform();
//Thread.sleep(3000);
//quit the browser  
driver.quit();  
}
} 
  • To run the test script in Eclipse, right-click on the window and then click Run as drag and drop event handling Java application and the test script will be launched in the chrome browser and automate all the test scenarios.
drag and drop event handling 9

Related Topics

Xpath locator in Selenium IDE

X PATH LOCATOR: Xpath is used to identify the element using any attribute or visible test. Syntax for X PATH:- //html tag [@attribute=’value’] e.g. - //div [@class=’label’]                    OR //html tag [text () =’Visible Text’] e.g.-//div [text...

2 minutes read.

Selenium WebDriver-Locators

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

1 minute read.

Selenium IDE Commands

Selenium –IDE Commands (SELENESE): Selenese commands are the set of commands used in Selenium IDE. Selenese command has two parameters target and value. Selenese command tests the existence of UI elements based on their HTML tags,...

3 minutes read.

Selenium WebDriver-CSS Selector: Tag and ID

Selenium WebDriver-CSS Selector: Tag and ID We will locate the web element with the help of tag and id CSS selector in this section of the tutorials Where Tag and id are...

4 minutes read.

CSS Selector: Attribute in Selenium IDE

CSS Selector: Attribute Locating the attribute of a CSS selector, we would access the Password textbox, which is present in the login form of Facebook. The Password textbox has a type attribute...

1 minute read.

Selenium-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute read.

Selenium Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

10 minutes read.

Selenium-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

1 minute read.

Selenium WebDriver- Checkbox handling

Selenium WebDriver- Checkbox handling In this tutorial, we will be learning about handling the checkbox in selenium WebDriver. Let us take one example to give you a better understanding of Checkbox handling,...

4 minutes read.

Selenium WebDriver - IE [Internet Explorer] browser

Selenium WebDriver - IE [Internet Explorer] browser In this segment, we will understand how to run a test script with the help of the IE (Internet Explorer) browser. It is a standalone...

4 minutes read.

CSS Selector Locator in Selenium IDE

It used to identify the web element based on HTML tag, id, class, and attributes. To describe the look and formatting of a document written in the markup language are known...

1 minute read.

Selenium WebDriver-CSS Selector locator

Selenium WebDriver-CSS Selector locator A CSS selector is used to identify the web element based on their “# id” and “. Class”, Html tags, and attributes. A CSS stands for Cascading Style...

1 minute read.

Advantages and Disadvantages of Selenium-IDE

Advantages and Disadvantages of Selenium-IDE Advantages of Selenium IDE: UI based record and playback tool. Easy to understand & developed a test script. Selenium-IDE test script export into web driver code & Selenium–RC It is...

1 minute read.

WebDriver-Class Name Locator

WebDriver-Class Name Locator In this tutorial, we will learn about the Class name locator of selenium WebDriver. The Class Name locator is used to identify any element in UI based on the...

4 minutes read.

Selenium WebDriver-CSS Selector: Tag and Attribute

Selenium WebDriver-CSS Selector: Tag and Attribute We will locate the web element with the help of Tag and attribute CSS selector in this section of the tutorial. Where Tag and attribute are...

5 minutes read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

Link Text Locator in Selenium IDE

LINK TEXT Link textlocator is used to identify only the weblink element based on the name of the link. Whenever multiple links are there, it will not work; it will work only...

1 minute read.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 minutes read.

Selenium Tool Suite

The selenium tool suite is a combination of multiple software tools, and the entire tools having a different approach to support automation testing. Selenium test tool suites are as follows- Selenium...

2 minutes read.

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

4 minutes read.