From 1723157528142356510154a2f2e4739ab5b64cfd Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 29 Nov 2008 12:55:57 -0500 Subject: [PATCH] Extracting SelectOptionLocator --- lib/webrat/core/locators.rb | 17 +++++------ lib/webrat/core/locators/link_locator.rb | 4 +-- .../core/locators/select_option_locator.rb | 30 +++++++++++++++++++ lib/webrat/core/scope.rb | 8 ++--- spec/api/select_spec.rb | 2 +- 5 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 lib/webrat/core/locators/select_option_locator.rb diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index c0ccb705..827ae4a3 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -65,20 +65,17 @@ def find_field_with_id(id, *field_types) #:nodoc: end def find_select_option(option_text, id_or_name_or_label) #:nodoc: - # TODO - Convert to using elements + require "webrat/core/locators/select_option_locator" + + option = SelectOptionLocator.new(self, option_text, id_or_name_or_label).locate + return option if option if id_or_name_or_label - field = field(id_or_name_or_label, SelectField) - return field.find_option(option_text) + select_box_text = " in the #{id_or_name_or_label.inspect} select box" + raise NotFoundError.new("The '#{option_text}' option was not found#{select_box_text}") else - select_option = forms.detect_mapped do |form| - form.find_select_option(option_text) - end - - return select_option if select_option + raise NotFoundError.new("Could not find option #{option_text.inspect}") end - - raise NotFoundError.new("Could not find option #{option_text.inspect}") end def find_button(value) #:nodoc: diff --git a/lib/webrat/core/locators/link_locator.rb b/lib/webrat/core/locators/link_locator.rb index 2097bc12..6bbd1ff5 100644 --- a/lib/webrat/core/locators/link_locator.rb +++ b/lib/webrat/core/locators/link_locator.rb @@ -10,7 +10,7 @@ def locate end def link_element - matching_links.min { |a, b| Webrat::XML.inner_text(a).length <=> Webrat::XML.inner_text(b).length } + matching_links.min { |a, b| Webrat::XML.all_inner_text(a).length <=> Webrat::XML.all_inner_text(b).length } end def matching_links @@ -27,7 +27,7 @@ def matches_text?(link) matcher = /#{Regexp.escape(@value.to_s)}/i end - replace_nbsp(Webrat::XML.inner_text(link)) =~ matcher || + replace_nbsp(Webrat::XML.all_inner_text(link)) =~ matcher || replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher || Webrat::XML.attribute(link, "title")=~ matcher end diff --git a/lib/webrat/core/locators/select_option_locator.rb b/lib/webrat/core/locators/select_option_locator.rb new file mode 100644 index 00000000..9be1d03d --- /dev/null +++ b/lib/webrat/core/locators/select_option_locator.rb @@ -0,0 +1,30 @@ +require "webrat/core/locators/locator" + +module Webrat + module Locators + + class SelectOptionLocator < Locator + + def initialize(scope, option_text, id_or_name_or_label) + @scope = scope + @option_text = option_text + @id_or_name_or_label = id_or_name_or_label + end + + def locate + # TODO - Convert to using elements + + if @id_or_name_or_label + field = @scope.field(@id_or_name_or_label, SelectField) + field.find_option(@option_text) + else + @scope.send(:forms).detect_mapped do |form| + form.find_select_option(@option_text) + end + end + end + + end + + end +end \ No newline at end of file diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index cf83ec2e..a49c2a78 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -100,12 +100,8 @@ def choose(field_locator) # select "February", :from => "event_month" # select "February", :from => "Event Month" def select(option_text, options = {}) - if option = find_select_option(option_text, options[:from]) - option.choose - else - select_box_text = options[:from] ? " in the '#{options[:from]}' select box" : '' - raise NotFoundError.new("The '#{option_text}' option was not found#{select_box_text}") - end + option = find_select_option(option_text, options[:from]) + option.choose end webrat_deprecate :selects, :select diff --git a/spec/api/select_spec.rb b/spec/api/select_spec.rb index 7af4b3a8..337e54b6 100644 --- a/spec/api/select_spec.rb +++ b/spec/api/select_spec.rb @@ -11,7 +11,7 @@ HTML lambda { select "February", :from => "month" }.should raise_error(Webrat::NotFoundError, - "The 'February' option was not found in the 'month' select box") + "The 'February' option was not found in the \"month\" select box") end it "should fail if option not found in list specified by element name" do