Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Drag and Drop using Selenium Webdriver for Ruby

Are there any work arounds to getting HTML5 Drag and Drop working with Selenium Webdriver with Ruby? I am using Selenium-Webdriver 2.20.0 with Ruby 1.9.2

Here is a simple test to reproduce the issue:

require "selenium-webdriver"
require "test/unit"

class Html5DragAndDropTest < Test::Unit::TestCase

  def setup
    @driver = Selenium::WebDriver.for :firefox
    @driver.manage.timeouts.implicit_wait = 30
  end

  def teardown
    @driver.quit
  end

  def test_html5_drag_and_drop
    @driver.get("http://html5demos.com/drag")
    target = @driver.find_element(:id, "one")
    source = @driver.find_element(:id, "bin")
    @driver.action.drag_and_drop(target, source).perform
    assert target.displayed? == false
  end
end
like image 516
Ryan Thomas Correia Ortega Avatar asked Mar 21 '12 01:03

Ryan Thomas Correia Ortega


People also ask

How do you drag and drop actions 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.

Can we use Ruby with selenium?

Selenium with Ruby. BrowserStack gives you instant access to our Selenium Grid of 3000+ real devices and desktop browsers. Running your Selenium tests with Ruby on BrowserStack is simple.

Is drag and drop possible in selenium?

Selenium offers a wide range of functionalities for automation testing of a website. One website feature that must be automated during testing is Drag and Drop. Selenium provides an easy way to drag a web element from one part of the site and drop it at another.

How do you write xpath for drag and drop?

First, we capture the 1st element which we need to drag in variable “From.” WebElement From=driver. findElement(By. xpath("//*[@id='credit2']/a"));


2 Answers

This is still a bug in Selenium, so the JavaScript workaround noted above is a good one.

I built an example HTML drag and drop page and wrote a test to exercise it using the drag_and_drop_helper.js gist Ryan provided. You can see my full write-up here.

Cheers,
Dave H
@TourDeDave

like image 128
Dave Haeffner Avatar answered Oct 12 '22 23:10

Dave Haeffner


Here is a temporary workaround that could help the community with testing in the meantime...

1) drag_and_drop_helper.js(https://gist.github.com/2362544) to your test/helpers directory

2) Create a new method in your test_helper.rb

 def drag_and_drop(source,target)

   js_filepath=File.dirname(__FILE__)+"/drag_and_drop_helper.js"
   js_file= File.new(js_filepath,"r")
   java_script=""

  while (line=js_file.gets)
    java_script+=line
   end

   js_file.close

   @driver.execute_script(java_script+"$('#{source}').simulateDragDrop({ dropTarget: '#{target}'});")

   rescue Exception => e
     puts "ERROR :" + e.to_s

end
like image 35
Ryan Thomas Correia Ortega Avatar answered Oct 12 '22 23:10

Ryan Thomas Correia Ortega