Changing Field creation to use XPath

This commit is contained in:
Bryan Helmkamp 2008-11-29 00:43:24 -05:00
parent 3134d1ce73
commit 6316faae44
2 changed files with 49 additions and 19 deletions

View File

@ -8,22 +8,6 @@ module Webrat
end end
class Field #:nodoc: class Field #:nodoc:
def self.class_for_element(element)
if element.name == "input"
if %w[submit image].include?(Webrat::XML.attribute(element, "type"))
field_class = "button"
else
field_class = Webrat::XML.attribute(element, "type") || "text" #default type; 'type' attribute is not mandatory
end
else
field_class = element.name
end
Webrat.const_get("#{field_class.capitalize}Field")
rescue NameError
raise "Invalid field element: #{element.inspect}"
end
attr_reader :value attr_reader :value
def initialize(form, element) def initialize(form, element)
@ -146,6 +130,10 @@ module Webrat
class ButtonField < Field #:nodoc: class ButtonField < Field #:nodoc:
def self.xpath_search
[".//button", ".//input[@type = 'submit']", ".//input[@type = 'image']"]
end
def to_param def to_param
return nil if @value.nil? return nil if @value.nil?
super super
@ -165,6 +153,10 @@ module Webrat
class HiddenField < Field #:nodoc: class HiddenField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'hidden']"
end
def to_param def to_param
if collection_name? if collection_name?
super super
@ -189,6 +181,10 @@ module Webrat
class CheckboxField < Field #:nodoc: class CheckboxField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'checkbox']"
end
def to_param def to_param
return nil if @value.nil? return nil if @value.nil?
super super
@ -221,10 +217,19 @@ module Webrat
end end
class PasswordField < Field #:nodoc: class PasswordField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'password']"
end
end end
class RadioField < Field #:nodoc: class RadioField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'radio']"
end
def to_param def to_param
return nil if @value.nil? return nil if @value.nil?
super super
@ -261,6 +266,10 @@ module Webrat
class TextareaField < Field #:nodoc: class TextareaField < Field #:nodoc:
def self.xpath_search
".//textarea"
end
protected protected
def default_value def default_value
@ -270,7 +279,11 @@ module Webrat
end end
class FileField < Field #:nodoc: class FileField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'file']"
end
attr_accessor :content_type attr_accessor :content_type
def set(value, content_type = nil) def set(value, content_type = nil)
@ -299,13 +312,23 @@ module Webrat
end end
class TextField < Field #:nodoc: class TextField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'text']", ".//input[not(@type)]"]
end
end end
class ResetField < Field #:nodoc: class ResetField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'reset']"
end
end end
class SelectField < Field #:nodoc: class SelectField < Field #:nodoc:
def self.xpath_search
".//select"
end
def find_option(text) def find_option(text)
options.detect { |o| o.matches_text?(text) } options.detect { |o| o.matches_text?(text) }
end end

View File

@ -43,9 +43,16 @@ module Webrat
def fields def fields
return @fields if @fields return @fields if @fields
@fields = Webrat::XML.css_search(@element, "button", "input", "textarea", "select").collect do |field_element| @fields = []
Field.class_for_element(field_element).new(self, field_element)
[SelectField, TextareaField, ButtonField, CheckboxField, PasswordField,
RadioField, FileField, ResetField, TextField, HiddenField].each do |field_class|
@fields += Webrat::XML.xpath_search(@element, *field_class.xpath_search).map do |element|
field_class.new(self, element)
end
end end
@fields
end end
def labels def labels