×

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

Characteristics of Selenium IDE

Characteristics of Selenium IDE Selenium IDE is separated into different parts, each having their features and functionalities. The seven various components of Selenium IDE are as follows: MENU BAR: The menu bar is...

3 minutes read.

Selenium WebDriver Characteristics

Characteristics of Selenium WebDriver Some of the essential characteristics of Selenium WebDriver are as follows: Multiple Operating System Support Selenium Webdriver supports multiple operating systems like: Desktop OS: Window, Linux, and macOS Mobile OS:...

2 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 WebDriver- Radio button handling

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

3 minutes read.

CSS Selector: Inner text in Selenium IDE

CSS Selector: Inner text The inner text is used to identify and create a CSS Selector using a string pattern of the HTML tag which is displayed on the web page. Syntax...

1 minute read.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 minutes read.

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

Operational Controls Methods in Selenium WebDriver

Operational Controls Methods The Operational controls are used to perform all the actions on the web elements like click on the button, pass the value in the text box, and click...

4 minutes read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

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-Alert Popup

Selenium WebDriver-Alert Popup  An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser. The Alert popup will be...

6 minutes read.

Selenium IDE Tutorial for Beginners

Selenium IDE Introduction Selenium IDE stands for Integrated Development Environment. Selenium-IDE is released in 2006 and developed by SHINYA [JAPAN] THOUGHTWORK Company. Selenium IDE implemented as a Firefox plug-in, and concedes the record, edit, and debug tests....

2 minutes read.

Selenium WebDriver - Google Chrome Browser

Selenium WebDriver - Google Chrome Browser In this tutorial, we will learn how to run our Selenium Test Scripts in the Google Chrome Browser. To implement the WebDriver protocol with the help...

3 minutes read.

Mouse and Keyboard Controls in Selenium WebDriver

Mouse and Keyboard Controls in Selenium WebDriver In this tutorial, we will learn about how to handle mouse and keyboard event using action class in selenium WebDriver. Action Class in Selenium WebDriver: Action...

13 minutes read.

First test case of Selenium-IDE

First Test Case of Selenium-IDE: In this part, we are discussing how to create a basic test case in Selenium-IDE: The entire test script execution process in Selenium -IDE can be divided...

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.

Selenium vs QTP

In this section, we are comparing the most frequently used automation tool –Selenium and QTP. Selenium and QTP have relative advantages over each other- FeaturesSeleniumHP QTPLicensingSelenium is an open-source testing tool....

3 minutes read.

WebDriver Interface | Class Diagram

WebDriver interface/class diagram: The interface of WebDriver includes the following stages, which is as follows: Search Context Web driver Remote web driver Driver class Search Context  The first interface of the web driver class is search Context,...

2 minutes read.

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes 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.