Selenium Webdriver Java - looking for alternatives for Actions and Robot when performing drag-and-drop

Posted by Ja-ke Alconcel on Stack Overflow See other posts from Stack Overflow or by Ja-ke Alconcel
Published on 2014-08-22T03:14:43Z Indexed on 2014/08/22 4:20 UTC
Read the original article Hit count: 205

Filed under:
|

I first tried Actions class and the drag-and-drop does work on different elements, however it was unable to locate the a specific draggable element on it's exact screen/webpage position.

Here's the code I've used:

    Point loc = driver.findElement(By.id("thiselement")).getLocation();
    System.out.println(loc);
    WebElement drag = driver.findElement(By.id("thiselement"));
    Actions test = new Actions(driver);
    test.dragAndDropBy(drag, 0, 60).build().perform();

I checked the element with it's pixel location and it prints (837, -52), which was somewhere on top of the webpage and was pixels away from the actual element.

Then I tried using the Robot class and works perfectly fine on my script, but can only provide constant successful runs on a single test machine, running it with a different machine with different screen resolution and screen size will render the script to fail due to the dependency of Robot on the pixel location of the element.

The sample code of the Robot script I'm using:

Robot dragAndDrop = new Robot();

  dragAndDrop.mouseMove(945, 166); //actual pixel location of the draggable element
  dragAndDrop.mousePress(InputEvent.BUTTON1_MASK);
        sleep(3000);
  dragAndDrop.mouseMove(945, 226);
  dragAndDrop.mouseRelease(InputEvent.BUTTON1_MASK);
        sleep(3000);

Is there any alternative for Actions and Robot to automate drag-and-drop? Or maybe a help on working the script to work on Actions as I really can't use Robot.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about java

Related posts about selenium-webdriver