Friday, 8 July 2016

Multiple ways of performing Drag and drop operations

1.Way:
=======
package com.rameshsoft.draganddrrop;

import java.awt.AWTException;
import java.util.List;
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;

public class DragDropOne {
public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://www.jqueryui.com");
        Actions actions=new Actions(driver);
        WebElement draggable=driver.findElement(By.linkText("Droppable"));
        draggable.click();  
        driver.switchTo().frame(0);      
        WebElement source = driver.findElement(By.id("draggable"));      
        WebElement target = driver.findElement(By.id("droppable"));        
        actions.dragAndDrop(source, target).build().perform();    
        driver.quit();
     
}
}

2.Way:
=======
package com.rameshsoft.draganddrrop;

import java.awt.AWTException;
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.Action;
import org.openqa.selenium.interactions.Actions;

public class DragAndDropTwo {

public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://www.jqueryui.com");
        Actions actions=new Actions(driver); 
        WebElement draggable=driver.findElement(By.linkText("Droppable"));
        draggable.click();       
        driver.switchTo().frame(0);
        WebElement source = driver.findElement(By.id("draggable"));       
        WebElement target = driver.findElement(By.id("droppable"));
        Actions builder = new Actions(driver);
        Action dragAndDrop = builder.clickAndHold(source).moveToElement(target).release(target).build();
        dragAndDrop.perform();      
        Thread.sleep(5000);
        driver.quit();
        
}
}

3.Way:
=======
package com.rameshsoft.draganddrrop;

import java.awt.AWTException;
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.Action;
import org.openqa.selenium.interactions.Actions;

public class DragAndDropThree {

public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://www.jqueryui.com");
        Actions actions=new Actions(driver); 
        WebElement draggable=driver.findElement(By.linkText("Droppable"));
        draggable.click();       
        driver.switchTo().frame(0);
        WebElement source = driver.findElement(By.id("draggable"));       
        WebElement target = driver.findElement(By.id("droppable"));        
        Actions builder = new Actions(driver);
        Action dragAndDrop =  builder.clickAndHold(source).moveToElement(target, 2, 2).release(target).build();
        dragAndDrop.perform();      
        Thread.sleep(5000);
        driver.quit();
        
}
}

4. Way:
=======
package com.rameshsoft.draganddrrop;

import java.awt.AWTException;
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.Action;
import org.openqa.selenium.interactions.Actions;

public class DragAndDropFour {

public static void main(String[] args) throws InterruptedException, AWTException {
        WebDriver driver=new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://www.jqueryui.com");
        Actions actions=new Actions(driver); 
        WebElement draggable=driver.findElement(By.linkText("Droppable"));
        draggable.click();       
        driver.switchTo().frame(0);
        WebElement source = driver.findElement(By.id("draggable"));       
        WebElement target = driver.findElement(By.id("droppable"));      
        Actions builder = new Actions(driver);
        builder.clickAndHold(source).moveToElement(target).perform();
        Thread.sleep(2000);
        builder.release(target).build().perform();      
        Thread.sleep(5000);
        driver.quit();
}
}

No comments:

Post a Comment