Wednesday, 25 April 2018

How to Perform Drag and Drop using Selenium Web Driver

We have already seen Actions class in Selenium and learnt basics about it. If you want Read Here for more details.Basically action class is used to perform various mouse and keyboard driven actions such as click and hold, mouse over etc. In this blog post we are going to use Actions class in Selenium to perform Drag and Drop actions. 

Many websites offers Drag and Drop and hence we will see how we can automate it.

Video Tutorial -


For Demo purpose I am using sample Drag and Drop -

https://jqueryui.com/droppable/

Lets see the simple Test case which performs drag and drop -


import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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 {
 
ChromeDriver driver;
 
 @Before
 public  void setup(){
   
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  
      driver=new ChromeDriver();

       driver.manage().window().maximize();
  
        // start the application
  
       driver.get("https://jqueryui.com/droppable/");
  
 }
 
 @Test
 public void testBootStrap() throws Exception
 {
 
  // Add 10 seconds wait
  Thread.sleep(10000);
   
  // Create object of actions class
  Actions act=new Actions(driver);
   
  driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
  // find element which we need to drag
  WebElement drag=driver.findElement(By.xpath("//*[@id='draggable']"));
   
  // find element which we need to drop
  WebElement drop=driver.findElement(By.xpath("//*[@id='droppable']"));
   
  // this will drag element to destination
  act.dragAndDrop(drag, drop).build().perform();
  
  // Add 2 seconds wait
  Thread.sleep(2000);
    }
 
@After
public void tearDown()
{ 
 driver.quit();
}


}

3 comments:

  1. It’s a great post. Keep sharing such kind of worthy information.

    Mobile Testing Training in Chennai | Mobile Testing Course in Chennai

    ReplyDelete
    Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. Nice Article.The step by step coding is also very helpful. thanks for posting!!

    Sushant Singh Rajput Case: Rhea Chakraborty is under surveillance by Bihar Police-The TrendyFeed News.

    Get TrendyFeed Latest Entertainment News

    The TrendyFeed News:
    THETRENDYFEED.COM is one of India's new business news publications - 60% of THETRENDYFEED.COM readers did not read another business paper, making TTF an important reach vehicle for top end audiences. TTF among business letters in reference to readers.

    For more information feel free to Contact Us
    Email us to contact us: - THETRENDYFEED@GMAIL.COM

    ReplyDelete