Working on SelectOptionLocator

This commit is contained in:
Bryan Helmkamp 2008-11-30 15:09:54 -05:00
parent e72bba29c7
commit ac4feb3b7e
7 changed files with 24 additions and 48 deletions

View File

@ -13,6 +13,8 @@ module Webrat
session.elements[Webrat::XML.xpath_to(element)] ||= self.new(session, element)
end
attr_reader :element
def initialize(session, element)
@session = session
@element = element

View File

@ -68,11 +68,6 @@ module Webrat
def matches_name?(name)
Webrat::XML.attribute(@element, "name") == name.to_s
end
def matches_label?(label_text)
return false if labels.empty?
labels.any? { |label| label.matches_text?(label_text) }
end
def disabled?
@element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false'

View File

@ -14,12 +14,9 @@ module Webrat
def find_select_option(option_text)
select_fields = fields_by_type([SelectField])
select_fields.each do |select_field|
result = select_field.find_option(option_text)
return result if result
select_fields.detect_mapped do |select_field|
select_field.find_option(option_text)
end
nil
end
def fields

View File

@ -32,18 +32,10 @@ module Webrat
else
matcher = /#{Regexp.escape(link_text.to_s)}/i
end
replace_nbsp(text) =~ matcher || replace_nbsp_ref(inner_html) =~ matcher || title =~ matcher
end
def matches_id?(id_or_regexp)
if id_or_regexp.is_a?(Regexp)
(id =~ id_or_regexp) ? true : false
else
(id == id_or_regexp) ? true : false
end
end
def inner_html
Webrat::XML.inner_html(@element)
end

View File

@ -7,14 +7,6 @@ module Webrat
".//option"
end
def matches_text?(text)
if text.is_a?(Regexp)
Webrat::XML.inner_html(@element) =~ text
else
Webrat::XML.inner_html(@element) == text.to_s
end
end
def choose
select.raise_error_if_disabled
select.set(value)

View File

@ -15,11 +15,27 @@ module Webrat
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)
field = FieldLocator.new(@scope, @id_or_name_or_label, SelectField).locate!
field.send(:options).detect do |o|
if @option_text.is_a?(Regexp)
Webrat::XML.inner_html(o.element) =~ @option_text
else
Webrat::XML.inner_html(o.element) == @option_text.to_s
end
end
else
@scope.send(:forms).detect_mapped do |form|
form.find_select_option(@option_text)
select_fields = form.send(:fields_by_type, [SelectField])
select_fields.detect_mapped do |select_field|
select_field.send(:options).detect do |o|
if @option_text.is_a?(Regexp)
Webrat::XML.inner_html(o.element) =~ @option_text
else
Webrat::XML.inner_html(o.element) == @option_text.to_s
end
end
end
end
end
end

View File

@ -78,23 +78,5 @@ describe Webrat::Link do
link.should_receive(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10
end
it "should matches_id? on exact matching id" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:id).and_return("some_id")
link.matches_id?("some_id").should == true
end
it "should not matches_id? on incorrect id" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:id).and_return("other_id")
link.matches_id?("some_id").should == false
end
it "should matches_id? on matching id by regexp" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:id).and_return("some_id")
link.matches_id?(/some/).should == true
end
end