Wednesday, 3 July 2013

Handling "drag and drop" actions using WebDriver(Selenium 2)



Automating rich web application is even more interesting since it involves advanced user interactions.

Say we have a web application which drag an item from one location and then drop it at another location.These kind of drag and drops are cant be automated with one single statement .In WebDriver we have a separatae "Actions" class to handle advanced user interactions(say drag and drop) on web page.

Here is the documentation on how to automate advanced user interactions :
http://code.google.com/p/selenium/wiki/AdvancedUserInteractions

Here is the sample code using TestNG framework
  1. import org.openqa.selenium.By;  
  2. import org.openqa.selenium.WebDriver;  
  3. import org.openqa.selenium.WebElement;  
  4. import org.openqa.selenium.firefox.FirefoxDriver;  
  5. import org.openqa.selenium.firefox.FirefoxProfile;  
  6. import org.openqa.selenium.interactions.Action;  
  7. import org.openqa.selenium.interactions.Actions;  
  8. import org.testng.annotations.BeforeTest;  
  9. import org.testng.annotations.Test;  
  10.   
  11. public class draganddrop {  
  12.    
  13.  WebDriver driver;  
  14.    
  15.  @BeforeTest  
  16.  public void start(){  
  17.   FirefoxProfile profile = new FirefoxProfile();  
  18.   profile.setEnableNativeEvents(true);  
  19.   driver = new FirefoxDriver(profile);  
  20.  }  
  21.   
  22.  @Test  
  23.  public void start1(){  
  24.   driver.get("http://jqueryui.com/droppable/");  
  25.   driver.switchTo().frame(0);    
  26.   driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);  
  27.   WebElement dragElement=driver.findElement(By.id("draggable"));  
  28.   WebElement dropElement=driver.findElement(By.id("droppable"));  
  29.       
  30.   Actions builder = new Actions(driver);  // Configure the Action  
  31.   Action dragAndDrop = builder.clickAndHold(dragElement)  
  32.     .moveToElement(dropElement)  
  33.     .release(dropElement)  
  34.     .build();  // Get the action  
  35.     dragAndDrop.perform(); // Execute the Action  
  36.  }  
  37. }  

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...