Permalink
Browse files

add drag and drop support

  • Loading branch information...
1 parent f387353 commit 78ebdc335db12c222f8630bf896c300454299cc8 @jarmo jarmo committed Mar 12, 2012
Showing with 62 additions and 6 deletions.
  1. +1 −0 watir/lib/watir/core.rb
  2. +57 −0 watir/lib/watir/drag_and_drop_helper.rb
  3. +4 −6 watir/lib/watir/element.rb
View
1 watir/lib/watir/core.rb
@@ -18,6 +18,7 @@
require 'watir/locator'
require 'watir/page-container'
require 'watir/ie-class'
+require 'watir/drag_and_drop_helper'
require 'watir/element'
require 'watir/element_collection'
require 'watir/form'
View
57 watir/lib/watir/drag_and_drop_helper.rb
@@ -0,0 +1,57 @@
+module Watir
+ module DragAndDropHelper
+
+ def drag_and_drop_on(target)
+ perform_action do
+ assert_target target
+ drop_x = target.send :left_edge_absolute
+ drop_y = target.send :top_edge_absolute
+ drag_to drop_x, drop_y
+ end
+ end
+
+ def drag_and_drop_by(distance_x, distance_y)
+ perform_action do
+ drag_x, drag_y = source_x_y
+ drag_to drag_x + distance_x, drag_y + distance_y
+ end
+ end
+
+ private
+
+ def drag_to(drop_x, drop_y)
+ drag_x, drag_y = source_x_y
+ mouse = page_container.rautomation.mouse
+ mouse.move :x => drag_x + 10, :y => drag_y + 10
+ mouse.press
+ mouse.move :x => drop_x + 10, :y => drop_y + 10
+ mouse.release
+ end
+
+ def source_x_y
+ return left_edge_absolute, top_edge_absolute
+ end
+
+ def assert_target(target)
+ target.assert_exists
+ target.assert_enabled
+ end
+
+ def top_edge
+ ole_object.getBoundingClientRect.top.to_i
+ end
+
+ def top_edge_absolute
+ top_edge + page_container.document.parentWindow.screenTop.to_i
+ end
+
+ def left_edge
+ ole_object.getBoundingClientRect.left.to_i
+ end
+
+ def left_edge_absolute
+ left_edge + page_container.document.parentWindow.screenLeft.to_i
+ end
+
+ end
+end
View
10 watir/lib/watir/element.rb
@@ -6,6 +6,8 @@ class Element # Wrapper
include ElementExtensions
include Exception
include Container # presumes @container is defined
+ include DragAndDropHelper
+
attr_accessor :container
# number of spaces that separate the property from the value in the to_s method
@@ -325,12 +327,8 @@ def fire_event(event)
def dispatch_event(event)
assert_exists
- if IE.version_parts.first.to_i >= 9
- if @container.page_container.document.documentMode.to_i >= 9
- ole_object.dispatchEvent(create_event(event))
- else
- ole_object.fireEvent(event)
- end
+ if IE.version_parts.first.to_i >= 9 && container.page_container.document.documentMode.to_i >= 9
+ ole_object.dispatchEvent(create_event(event))
else
ole_object.fireEvent(event)
end

0 comments on commit 78ebdc3

Please sign in to comment.