26
Jun

drag and drop with selenium webdriver

package SeleniumPractice;

import java.text.ParseException;
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.testng.Assert;
import org.testng.annotations.Test;

public class DragAndDropInSelenium {
WebDriver driver;

@Test
public void enterText() throws InterruptedException, ParseException {

// First step set the driver location
System.setProperty(“webdriver.chrome.driver”, “C:\\softwares\\Selenium jars\\chromedriver.exe”);

// create chrome object
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(“http://jqueryui.com/droppable/”);
Thread.sleep(7000);
// driver.navigate().refresh();
// Thread.sleep(5000);
driver.switchTo().frame(driver.findElement(By.xpath(“//*[@id=’content’]/iframe”)));
WebElement sourceObject = driver.findElement(By.cssSelector(“#draggable”));
WebElement targetObject = driver.findElement(By.cssSelector(“#droppable”));
Actions action = new Actions(driver);
action.dragAndDrop(sourceObject, targetObject).build().perform();
Assert.assertEquals(targetObject.getText(), “Dropped!”);
}

}