0

Is there any examples of Javascript or jQuery been used with Selenium Webdriver Java? I'm having issues with drag and drop using selenium. I'm hoping to use the following code with Javascript/jQuery to perform a drag and drop. I cannot use point and click in my test scripts to perform drag and drop so I'm hoping to use jQuery or java script to perform the drag and drop. I'm having trouble integrating the two. I only want suggestion for drag and drop using javascript or an example of javascript (any) and selenium webdriver code in the same script

public void test(){                                           
    driver.findElement(By.id("addService")).click();
    driver.findElement(By.id("name")).sendKeys(name);
    driver.findElement(By.id("identifier")).sendKeys(id);
    driver.findElement(By.id("flowStatus")).clear();
    driver.findElement(By.id("flowStatus")).sendKeys(flow);
}

public void dragAndDropElement(WebElement dragFrom, WebElement dragTo) throws Exception {


    Actions actions = new Actions(driver);


     actions.clickAndHold(dragFrom).release(dragTo).build().perform();;
}

public void test() throws Exception {


    WebElement dragFrom = driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/form/fieldset/table[1]/tbody/tr/td[1]/div/div[1]"));
    WebElement dragTo = driver.findElement(By.id("drop"));

    dragAndDropElement(dragFrom,dragTo);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TrevDroid
  • 85
  • 3
  • 11
  • is this what you want? http://stackoverflow.com/questions/14210051/how-to-automate-drag-drop-functionality-using-selenium-web-driver – 97ldave Feb 11 '15 at 14:16
  • No I've tried all that really just looking for information on using selenium webdriver with javascript – TrevDroid Feb 11 '15 at 15:46

1 Answers1

0

Have tried using Actions class? Here are docs. There are multiple ways you could drag and drop elements using it,

 Actions actions = new Actions(driver);
 actions.dragAndDrop(source,target).build().perform();

or

 actions.clickAndHold(source).release(target).build().perform();

There are other ways as well, check docs and see which one is applicable for you. In general using javascript should be avoided when using WebDriver. WebDriver uses browsers native api's and simulate user interactions which are very close to a real user. Think of this, your user is not going to execute a javascript to do a drag and drop. I would suggest use it only when all other WebDriver doors are closed for you.

nilesh
  • 14,131
  • 7
  • 65
  • 79
  • I've tried this and it does not work I'm trying to move an item from one div to another. I've tried using this and it does not work – TrevDroid Feb 12 '15 at 09:27