Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Drag and Drop Example in Selenium Web Driver:-




Drag and drop is mostly used to create UI from the backend or dashboard part.

We can easily create a test script to implement drag and drop operation using 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();
       
}

}


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post