Friday, 8 May 2015

Selenium:How to perform drag and drop operations using selenium web driver

package New;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class DragDrop {
    WebDriver driver;
  @Test
  public void dragdrop() throws InterruptedException {
      driver.get("http://jqueryui.com/");
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      driver.findElement(By.linkText("Draggable")).click();
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      //driver.switchTo().frame("demo-frame");
     driver.switchTo().frame(0);
     // driver.switchTo().frame(By.name("demo-frame"));
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      WebElement dd=driver.findElement(By.id("draggable"));
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      Actions a=new Actions(driver);
      a.dragAndDropBy(dd, 100, 100).build().perform();
      Thread.sleep(2000);
      driver.switchTo().defaultContent();
      driver.findElement(By.linkText("Droppable")).click();
      driver.switchTo().frame(0);
     // WebElement source=driver.findElement(By.xpath("//div[text()='Drag me to my target']"));
     // WebElement dest=driver.findElement(By.xpath("//div[text()='Drop here']"));
     // WebElement source=driver.findElement(By.id("draggable"));
     // WebElement dest=driver.findElement(By.id("droppable"));
      WebElement source=driver.findElement(By.xpath("//div[@id='draggable']"));
      WebElement dest=driver.findElement(By.xpath("//div[@id='droppable']"));
      a.dragAndDrop(source, dest).build().perform();
      Thread.sleep(5000);
  }
  @BeforeClass
  public void beforeClass() {
      driver=new FirefoxDriver();
      driver.manage().window().maximize();
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
     
  }

  @AfterClass
  public void afterClass() {
      driver.quit();
     
  }

}

No comments:

Post a Comment