From 830b459482d6f9787cbee0ab48ba7e30c3771141 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 30 Nov 2008 01:26:07 -0500 Subject: [PATCH] SelectOption is a Webrat::Element too --- lib/webrat/core.rb | 2 +- lib/webrat/core/elements/field.rb | 2 +- lib/webrat/core/elements/select_option.rb | 39 +++++++++++++++++++++++ lib/webrat/core/select_option.rb | 29 ----------------- 4 files changed, 41 insertions(+), 31 deletions(-) create mode 100644 lib/webrat/core/elements/select_option.rb delete mode 100644 lib/webrat/core/select_option.rb diff --git a/lib/webrat/core.rb b/lib/webrat/core.rb index db5682d..74da7f7 100644 --- a/lib/webrat/core.rb +++ b/lib/webrat/core.rb @@ -7,7 +7,7 @@ require "webrat/core/scope" require "webrat/core/elements/link" require "webrat/core/elements/area" require "webrat/core/elements/label" -require "webrat/core/select_option" +require "webrat/core/elements/select_option" require "webrat/core/session" require "webrat/core/methods" require "webrat/core/matchers" diff --git a/lib/webrat/core/elements/field.rb b/lib/webrat/core/elements/field.rb index a1719b1..20ff342 100644 --- a/lib/webrat/core/elements/field.rb +++ b/lib/webrat/core/elements/field.rb @@ -369,7 +369,7 @@ module Webrat end def options - option_elements.map { |oe| SelectOption.new(self, oe) } + option_elements.map { |oe| SelectOption.new(@session, oe) } end def option_elements diff --git a/lib/webrat/core/elements/select_option.rb b/lib/webrat/core/elements/select_option.rb new file mode 100644 index 0000000..d10a153 --- /dev/null +++ b/lib/webrat/core/elements/select_option.rb @@ -0,0 +1,39 @@ +require "webrat/core/elements/element" + +module Webrat + class SelectOption < Element #:nodoc: + + 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) + end + + protected + + def select + @session.element_to_webrat_element(select_element) + end + + def select_element + parent = @element.parent + + while parent.respond_to?(:parent) + return parent if parent.name == 'select' + parent = parent.parent + end + end + + def value + Webrat::XML.attribute(@element, "value") || Webrat::XML.inner_html(@element) + end + + end +end \ No newline at end of file diff --git a/lib/webrat/core/select_option.rb b/lib/webrat/core/select_option.rb deleted file mode 100644 index 1a2001f..0000000 --- a/lib/webrat/core/select_option.rb +++ /dev/null @@ -1,29 +0,0 @@ -module Webrat - class SelectOption #:nodoc: - - def initialize(select, element) - @select = select - @element = element - 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) - end - - protected - - def value - Webrat::XML.attribute(@element, "value") || Webrat::XML.inner_html(@element) - end - - end -end \ No newline at end of file