Wednesday, 25 April 2018

How to Perform Drag and Drop using Selenium Web Driver

We have already seen Actions class in Selenium and learnt basics about it. If you want Read Here for more details.Basically action class is used to perform various mouse and keyboard driven actions such as click and hold, mouse over etc. In this blog post we are going to use Actions class in Selenium to perform Drag and Drop actions. 

Many websites offers Drag and Drop and hence we will see how we can automate it.

Video Tutorial -

For Demo purpose I am using sample Drag and Drop -

Lets see the simple Test case which performs drag and drop -

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class DragAndDrop {
ChromeDriver driver;
 public  void setup(){
  System.setProperty("", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
      driver=new ChromeDriver();

        // start the application
 public void testBootStrap() throws Exception
  // Add 10 seconds wait
  // Create object of actions class
  Actions act=new Actions(driver);
  // find element which we need to drag
  WebElement drag=driver.findElement(By.xpath("//*[@id='draggable']"));
  // find element which we need to drop
  WebElement drop=driver.findElement(By.xpath("//*[@id='droppable']"));
  // this will drag element to destination
  act.dragAndDrop(drag, drop).build().perform();
  // Add 2 seconds wait
public void tearDown()


1 comment: