15

I would like to drag-and-drop one element to the position of another, triggered from within a watir-webdriver script.

By "drag-and-drop" I mean picking up a draggable element and releasing it on another. By "possible" I mean any method for drag/drop that can be executed from a watir-webdriver script. This includes code snippets, third party gems, etc.

As I understand it drag-and-drop is a feature request for core watir-webdriver (at time of asking), so I'm looking (in principle) for an alternative.

UPDATE drag-and-drop is now part of core watir-webdriver (as of 0.5.0, I believe)

UPDATE 2 For those seeking enlightenment, this is now possible (as of version 0.5.0):

a = browser.div(:id => "draggable")
b = browser.div(:id => "droppable")

a.drag_and_drop_on b

and

a = browser.div(:id => "draggable")

a.drag_and_drop_by 100, -200
kinofrost
  • 768
  • 6
  • 16
  • This is something that would help me a lot as well, I'll be interested in how you get on. – Alastair Montgomery Jun 28 '11 at 09:55
  • Well watir-webdriver doesn't use OLE objects, so I can't create a hardware Win32API alternative. It's impossible to get a reliable x, y absolute coordinate for Win32API to use (even zeroing the window to 0,0 and using .location_once_scrolled_into_view). The only solution I know is as below, and works (or appears to) in IE, although I'm having trouble getting it to trigger actual drops in a ZK front-end. Hope that gives other drag/droppers a shortcut in their searching. – kinofrost Oct 24 '11 at 11:23

3 Answers3

9

I don't know if you found the answer for this by now, but this is how I do it for Firefox:

my_element.fire_event("onmousedown")
driver=browser.driver
driver.action.click_and_hold(my_element.wd).perform

sleep 2
driver.action.move_to(target.wd).perform

sleep 2
my_element.fire_event("onmouseup")

It fails without the delays, but it works fine with them on FF5.

GMD
  • 658
  • 5
  • 12
  • Thanks for your input! I have a solution for FF, although this one is better, so when it comes to cross-platform I'll definitely make use of it, so thank you. The companies that use our software mainly use IE for their workstations however, so I'm still looking for an IE solution (so far it involves the word "manual"). – kinofrost Aug 22 '11 at 08:23
  • This appears to work. You may find that it "drops" at the edge of the target, in which case you may also find driver.action.move_by(x, y) very useful! – kinofrost Oct 24 '11 at 09:19
  • This is something still helpful after 3 years :) thanks @GMD! – gouravtiwari21 May 21 '14 at 15:20
1
require 'rubygems'
require 'watir-webdriver'

module Watir
  class Element
    def drag_and_drop_on(other)
      assert_exists
      driver.action.drag_and_drop(@element, other.wd).perform
    end
  end
end

profile = Selenium::WebDriver::Firefox::Profile.new
profile.native_events = true

b = Watir::Browser.new :firefox, :profile => profile
b.goto "http://jqueryui.com/demos/droppable/default.html"

b.element(:id => "draggable").drag_and_drop_on(b.element(:id => "droppable"))

h3manth.com

hemanth.hm
  • 3,969
  • 2
  • 19
  • 10
  • Ta muchly! Should mention: Watir Webdriver now supports drag and drop as part of core functionality for non-firefox browsers, I'll add more code in my update – kinofrost Apr 17 '12 at 08:35
1

Did not use it myself, but there is some documentation about using Using drag and drop here: https://github.com/SeleniumHQ/selenium/wiki/Tips-And-Tricks

Željko Filipin
  • 52,828
  • 28
  • 91
  • 124