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
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
def initialize(form, element)
@ -146,6 +130,10 @@ module Webrat
class ButtonField < Field #:nodoc:
def self.xpath_search
[".//button", ".//input[@type = 'submit']", ".//input[@type = 'image']"]
end
def to_param
return nil if @value.nil?
super
@ -165,6 +153,10 @@ module Webrat
class HiddenField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'hidden']"
end
def to_param
if collection_name?
super
@ -189,6 +181,10 @@ module Webrat
class CheckboxField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'checkbox']"
end
def to_param
return nil if @value.nil?
super
@ -221,10 +217,19 @@ module Webrat
end
class PasswordField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'password']"
end
end
class RadioField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'radio']"
end
def to_param
return nil if @value.nil?
super
@ -261,6 +266,10 @@ module Webrat
class TextareaField < Field #:nodoc:
def self.xpath_search
".//textarea"
end
protected
def default_value
@ -270,7 +279,11 @@ module Webrat
end
class FileField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'file']"
end
attr_accessor :content_type
def set(value, content_type = nil)
@ -299,13 +312,23 @@ module Webrat
end
class TextField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'text']", ".//input[not(@type)]"]
end
end
class ResetField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'reset']"
end
end
class SelectField < Field #:nodoc:
def self.xpath_search
".//select"
end
def find_option(text)
options.detect { |o| o.matches_text?(text) }
end

View File

@ -43,9 +43,16 @@ module Webrat
def fields
return @fields if @fields
@fields = Webrat::XML.css_search(@element, "button", "input", "textarea", "select").collect do |field_element|
Field.class_for_element(field_element).new(self, field_element)
@fields = []
[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
@fields
end
def labels