From 4a55c1a7cb7adbae05cffb61bc9886223c00b4c9 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Wed, 9 Dec 2009 09:34:12 +0000 Subject: [PATCH 1/4] small refactor --- lib/capybara/driver/selenium_driver.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/capybara/driver/selenium_driver.rb b/lib/capybara/driver/selenium_driver.rb index b5b8db5b..af5dc537 100644 --- a/lib/capybara/driver/selenium_driver.rb +++ b/lib/capybara/driver/selenium_driver.rb @@ -17,16 +17,13 @@ class Capybara::Driver::Selenium end def set(value) - if tag_name == 'input' and %w(text password hidden file).include?(type) + if tag_name == 'textarea' or (tag_name == 'input' and %w(text password hidden file).include?(type)) node.clear node.send_keys(value.to_s) elsif tag_name == 'input' and type == 'radio' node.select elsif tag_name == 'input' and type == 'checkbox' node.toggle - elsif tag_name == "textarea" - node.clear - node.send_keys(value.to_s) end end From b93e1cd5b55b085cc1e83b382dd388e344c6cfd3 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Wed, 9 Dec 2009 09:34:42 +0000 Subject: [PATCH 2/4] Add drag API (subject to change), fix bug in has_content? --- lib/capybara/dsl.rb | 2 +- lib/capybara/session.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/capybara/dsl.rb b/lib/capybara/dsl.rb index 437ddfb4..21d13e81 100644 --- a/lib/capybara/dsl.rb +++ b/lib/capybara/dsl.rb @@ -47,7 +47,7 @@ module Capybara end SESSION_METHODS = [ - :visit, :body, :click_link, :click_button, :fill_in, :choose, :has_xpath?, :has_css?, + :visit, :body, :click_link, :click_button, :drag, :fill_in, :choose, :has_xpath?, :has_css?, :check, :uncheck, :attach_file, :select, :has_content?, :within, :within_fieldset, :within_table, :save_and_open_page, :find_field, :find_link, :find_button, :field_labeled diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 84e84c4b..12015376 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -44,6 +44,14 @@ module Capybara find_button(locator).click end + def drag(source_locator, target_locator) + source = find(source_locator).first + raise Capybara::ElementNotFound, "drag source '#{source_locator}' not found on page" unless source + target = find(target_locator).first + raise Capybara::ElementNotFound, "drag target '#{target_locator}' not found on page" unless target + source.drag_to(target) + end + def fill_in(locator, options={}) find_field(locator, :text_field, :text_area, :password_field).set(options[:with]) end @@ -73,7 +81,7 @@ module Capybara end def has_content?(content) - has_xpath?("//*[contains(.,#{sanitized_xpath_string(content)})]") + has_xpath?("[contains(.,#{sanitized_xpath_string(content)})] | //*[contains(.,#{sanitized_xpath_string(content)})]") end def has_xpath?(path, options={}) From c8cb9c32bf009e2b65ded718bca1beacf3554032 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Fri, 11 Dec 2009 11:49:54 +0000 Subject: [PATCH 3/4] Fix bug introduced in grag when the find API changed. --- lib/capybara/session.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 417b1118..320aebc8 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -49,9 +49,9 @@ module Capybara end def drag(source_locator, target_locator) - source = find(source_locator).first + source = find(source_locator) raise Capybara::ElementNotFound, "drag source '#{source_locator}' not found on page" unless source - target = find(target_locator).first + target = find(target_locator) raise Capybara::ElementNotFound, "drag target '#{target_locator}' not found on page" unless target source.drag_to(target) end From d0a0b6c68fe827faf3f7921c2514153f5d1c5356 Mon Sep 17 00:00:00 2001 From: Rob Holland Date: Fri, 11 Dec 2009 12:31:57 +0000 Subject: [PATCH 4/4] Add a spec for a scoped has_content corner case. Make the code pass. Remove duplicated function definition. --- lib/capybara/session.rb | 13 +------------ spec/session_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 320aebc8..bebee34c 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -97,7 +97,7 @@ module Capybara end def has_content?(content) - has_xpath?("[contains(.,#{sanitized_xpath_string(content)})] | //*[contains(.,#{sanitized_xpath_string(content)})]") + has_xpath?("/descendant-or-self::*[contains(.,#{sanitized_xpath_string(content)})]") end def has_xpath?(path, options={}) @@ -216,16 +216,5 @@ module Capybara "'#{string}'" end end - - def sanitized_xpath_string(string) - if string.include?("'") - string = string.split("'", -1).map do |substr| - "'#{substr}'" - end.join(%q{,"'",}) - "concat(#{string})" - else - "'#{string}'" - end - end end end diff --git a/spec/session_spec.rb b/spec/session_spec.rb index 2b3d5910..256f9109 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -362,6 +362,13 @@ shared_examples_for "session" do @session.should have_content('Redirect') end + it "should be true if scoped to an element which has the content" do + @session.visit('/with_html') + @session.within("//a[@title='awesome title']") do + @session.should have_content('labore') + end + end + it "should be false if the given content is not on the page" do @session.visit('/with_html') @session.should_not have_content('xxxxyzzz')