Extracting SelectOptionLocator

This commit is contained in:
Bryan Helmkamp 2008-11-29 12:55:57 -05:00
parent a472bbfbde
commit 1723157528
5 changed files with 42 additions and 19 deletions

View File

@ -65,20 +65,17 @@ module Webrat
end end
def find_select_option(option_text, id_or_name_or_label) #:nodoc: 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 if id_or_name_or_label
field = field(id_or_name_or_label, SelectField) select_box_text = " in the #{id_or_name_or_label.inspect} select box"
return field.find_option(option_text) raise NotFoundError.new("The '#{option_text}' option was not found#{select_box_text}")
else else
select_option = forms.detect_mapped do |form| raise NotFoundError.new("Could not find option #{option_text.inspect}")
form.find_select_option(option_text)
end
return select_option if select_option
end end
raise NotFoundError.new("Could not find option #{option_text.inspect}")
end end
def find_button(value) #:nodoc: def find_button(value) #:nodoc:

View File

@ -10,7 +10,7 @@ module Webrat
end end
def link_element 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 end
def matching_links def matching_links
@ -27,7 +27,7 @@ module Webrat
matcher = /#{Regexp.escape(@value.to_s)}/i matcher = /#{Regexp.escape(@value.to_s)}/i
end 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 || replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher ||
Webrat::XML.attribute(link, "title")=~ matcher Webrat::XML.attribute(link, "title")=~ matcher
end end

View File

@ -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

View File

@ -100,12 +100,8 @@ module Webrat
# select "February", :from => "event_month" # select "February", :from => "event_month"
# select "February", :from => "Event Month" # select "February", :from => "Event Month"
def select(option_text, options = {}) def select(option_text, options = {})
if option = find_select_option(option_text, options[:from]) option = find_select_option(option_text, options[:from])
option.choose 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
end end
webrat_deprecate :selects, :select webrat_deprecate :selects, :select

View File

@ -11,7 +11,7 @@ describe "select" do
HTML HTML
lambda { select "February", :from => "month" }.should raise_error(Webrat::NotFoundError, 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 end
it "should fail if option not found in list specified by element name" do it "should fail if option not found in list specified by element name" do