Double Clicking in Capybara

Tim and I spent a large chunk of yesterday hunting down an Internal Chrome Error in our test suite. The error cropped up after we added some double-click functionality to the app (chromedriver 14.x exhibited itself as a Timeout).

Here’s a quick recap of the hunt. The first iteration of our step looked like this:

When /^(?:|I )double[-\s]?click "([^"]*)"$/ do |locator|
  page.evaluate_script(%Q|$("a:contains('#{locator}')").dblclick()|)
end

As a first pass on cleaning this up, we tried changing evaluate_script to execute_script. We don’t care what that returns, we just want to trigger the double click event. But that still didn’t work.

So on we went, trudging through the internals of selenium-webdriver and capybara, trying to see if the double click functionality is exposed anywhere. Turns out, it is; you can find it in the Advanced User Interactions, exposed through the ActionBuilder.

Knowing that, we set out to revise the step:

When /^(?:|I )double[-\s]?click "([^"]*)"$/ do |locator|
  element = page.find(:xpath,"//a[contains(.,'#{locator}')]")
  page.driver.browser.mouse.double_click(element.native)
end

And with that, we killed the beast. Our tests were back to their pervious states. No more Internal Chrome Errors. And we lived happily thereafter.

bryckbost@gmail.com

Comments

  1. August 26, 2011 at 20:49 PM

    Yay! You guys rock. Sorry for adding that evil step in the first place, but it worked at the time. :/

  2. August 29, 2011 at 13:04 PM

    @Daniel Morrison: Just need to dive in and see what it would take to make it work in capybara-webkit :P

  3. ismyhc@gmail.com
    Jacob
    December 09, 2011 at 2:36 AM

    I am able to get the execute_script method to work when trying to doubleclick, but Im having trouble understanding what else needs to be done to enable the following line to work. I am new to capybara and ruby on rails for that matter!

    page.driver.browser.mouse.double_click(element.native)

  4. October 18, 2013 at 12:42 PM

    Great! Just the tip I was looking for and it works flawlessly. Thanks, Brian!