Extracting SelectOptionLocator
This commit is contained in:
parent
a472bbfbde
commit
1723157528
@ -65,21 +65,18 @@ module Webrat
|
||||
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
|
||||
end
|
||||
|
||||
raise NotFoundError.new("Could not find option #{option_text.inspect}")
|
||||
end
|
||||
end
|
||||
|
||||
def find_button(value) #:nodoc:
|
||||
require "webrat/core/locators/button_locator"
|
||||
|
@ -10,7 +10,7 @@ module Webrat
|
||||
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 @@ module Webrat
|
||||
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
|
||||
|
30
lib/webrat/core/locators/select_option_locator.rb
Normal file
30
lib/webrat/core/locators/select_option_locator.rb
Normal 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
|
@ -100,12 +100,8 @@ module Webrat
|
||||
# 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 = 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
|
||||
end
|
||||
|
||||
webrat_deprecate :selects, :select
|
||||
|
@ -11,7 +11,7 @@ describe "select" do
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user