Selenium Assignment – 7 – Drag and Drop

In this Selenium Assignment, we will be practicing iframe handling, CSS property verification and a mouse hover operation – drop and drop operation using actions class.

Scenario:

  • Open a browser of your choice for example – Chrome Browser.
  • Navigate to http://jqueryui.com/droppable/ webpage.

drag and drop using selenium

  • Consider “Drag me to my target” as a source and “Drop here” as a target.
  • Write a code to perform drag and drop operation from source to target.

Drag and Drop operation

  • After drag and drop verify the operation is successfully by checking the color property of CSS and also verify text change. (Use assert statement to verify that color and text are as expected.)

Try this scenario and send us the code for feedback and assessment, or if you have any questions, queries or comments. Feel free to write us saurabh@qatechhub.com. We will be back soon with the next assignment.

Till then, Keep Learning 🙂

Update — Please find the code below —

package day2;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class DragAndDrop {

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

		ChromeDriver driver;

		System.setProperty("webdriver.chrome.driver", "D:/Software/chromedriver_75/chromedriver.exe");

		driver = new ChromeDriver();

		driver.manage().window().maximize();

		driver.manage().deleteAllCookies();

		driver.get("https://jqueryui.com/droppable/");
		
		WebElement webFrame = driver.findElement(By.className("demo-frame"));
		
		driver.switchTo().frame(webFrame);
		
		WebElement source = driver.findElement(By.id("draggable"));
		WebElement target = driver.findElement(By.id("droppable"));
		
		Actions action = new Actions(driver);
		
		String colourBeforeDnD = target.getCssValue("color");
		
		//action.dragAndDrop(source, target).build().perform();
		
		action.moveToElement(source).clickAndHold().moveToElement(target).release().build().perform();

		String colorAfterDnD = target.getCssValue("color");
		
		System.out.println("Color before Drag and Drop : "+ colourBeforeDnD);
		
		System.out.println("Color after Drag and Drop : "+ colorAfterDnD);
	}

}

 

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: