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.
Comments
Yay! You guys rock. Sorry for adding that evil step in the first place, but it worked at the time. :/
@Daniel Morrison: Just need to dive in and see what it would take to make it work in capybara-webkit :P
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)
Great! Just the tip I was looking for and it works flawlessly. Thanks, Brian!