Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is drag-and-drop possible in watir-webdriver?

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
like image 787
kinofrost Avatar asked Jun 28 '11 08:06

kinofrost


People also ask

How do I dragAndDrop in Selenium Webdriver?

We can perform drag and drop action in Selenium with the help of Actions class. In order to perform the drag and drop movement we will use dragAndDrop (source, target) method. Finally use build(). perform() to execute all the steps.

Does Watir use Selenium?

Watir is an open-source web application testing framework that is designed to make writing Selenium tests simple and efficient. Built on Selenium's Ruby language bindings, Watir is able to drive the browser in the same way humans do.

How do you select an element in Watir?

Elements are located by creating a Selector Hash, which Watir translates into the potentially complicated information the driver needs to know to identify the element. The special cases will be highlighted below, but Watir Locators: Accept String values for exact matching. Accept RegExp values for partial matching.


2 Answers

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.

like image 152
GMD Avatar answered Sep 21 '22 15:09

GMD


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

like image 42
hemanth.hm Avatar answered Sep 21 '22 15:09

hemanth.hm