From 0aa6aa5b437d8fee71ba7becadf107fa5c6523d2 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Fri, 10 May 2013 09:55:17 -0700 Subject: [PATCH] Add double_click and right_click to element - support in selenium --- lib/capybara/driver/node.rb | 10 +++++++++- lib/capybara/node/element.rb | 16 ++++++++++++++++ lib/capybara/selenium/node.rb | 8 ++++++++ lib/capybara/spec/public/test.js | 7 +++++++ lib/capybara/spec/session/node_spec.rb | 16 ++++++++++++++++ lib/capybara/spec/views/with_js.erb | 2 ++ 6 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/capybara/driver/node.rb b/lib/capybara/driver/node.rb index 7ecf8739..8cc2eeff 100644 --- a/lib/capybara/driver/node.rb +++ b/lib/capybara/driver/node.rb @@ -40,7 +40,15 @@ module Capybara def click raise NotImplementedError end - + + def right_click + raise NotImplmentedError + end + + def double_click + raise NotImplementedError + end + def hover raise NotImplementedError end diff --git a/lib/capybara/node/element.rb b/lib/capybara/node/element.rb index 9b77fd53..3eb693ed 100644 --- a/lib/capybara/node/element.rb +++ b/lib/capybara/node/element.rb @@ -118,6 +118,22 @@ module Capybara synchronize { base.click } end + ## + # + # Right Click the Element + # + def right_click + synchronize { base.right_click } + end + + ## + # + # Double Click the Element + # + def double_click + synchronize { base.double_click } + end + ## # # Hover on the Element diff --git a/lib/capybara/selenium/node.rb b/lib/capybara/selenium/node.rb index 1de5bbee..33840b42 100644 --- a/lib/capybara/selenium/node.rb +++ b/lib/capybara/selenium/node.rb @@ -57,6 +57,14 @@ class Capybara::Selenium::Node < Capybara::Driver::Node def click native.click end + + def right_click + driver.browser.action.context_click(native).perform + end + + def double_click + driver.browser.action.double_click(native).perform + end def hover driver.browser.action.move_to(native).perform diff --git a/lib/capybara/spec/public/test.js b/lib/capybara/spec/public/test.js index 5980e6d7..89390ff1 100644 --- a/lib/capybara/spec/public/test.js +++ b/lib/capybara/spec/public/test.js @@ -60,4 +60,11 @@ $(function() { $('title').text('changed title') }, 250) }); + $('#click-test').dblclick(function() { + $(this).after('Has been double clicked'); + }); + $('#click-test').bind('contextmenu', function(e) { + e.preventDefault(); + $(this).after('Has been right clicked'); + }); }); diff --git a/lib/capybara/spec/session/node_spec.rb b/lib/capybara/spec/session/node_spec.rb index c015c8e7..08ace5c4 100644 --- a/lib/capybara/spec/session/node_spec.rb +++ b/lib/capybara/spec/session/node_spec.rb @@ -185,6 +185,22 @@ Capybara::SpecHelper.spec "node" do @session.find(:css, '.hidden_until_hover', :visible => false).should be_visible end end + + describe '#double_click', :requires => [:js] do + it "should double click an element" do + @session.visit('/with_js') + @session.find(:css, '#click-test').double_click + @session.find(:css, '#has-been-double-clicked').should be + end + end + + describe '#right_click', :requires => [:js] do + it "should double click an element" do + @session.visit('/with_js') + @session.find(:css, '#click-test').right_click + @session.find(:css, '#has-been-right-clicked').should be + end + end describe '#reload', :requires => [:js] do context "without automatic reload" do diff --git a/lib/capybara/spec/views/with_js.erb b/lib/capybara/spec/views/with_js.erb index 1fdb2284..82336ed4 100644 --- a/lib/capybara/spec/views/with_js.erb +++ b/lib/capybara/spec/views/with_js.erb @@ -61,6 +61,8 @@

Change title

+ +

Click me