Skip to content

Commit

Permalink
Extracting SelectOptionLocator
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 29, 2008
1 parent a472bbf commit 1723157
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
17 changes: 7 additions & 10 deletions lib/webrat/core/locators.rb
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions lib/webrat/core/locators/link_locator.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions 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
8 changes: 2 additions & 6 deletions lib/webrat/core/scope.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/api/select_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 1723157

Please sign in to comment.