Extracting Webrat::Element superclass and refactoring element creation
This commit is contained in:
parent
4594a39327
commit
3fd91e34be
@ -1,13 +1,10 @@
|
||||
require "webrat/core/element"
|
||||
|
||||
module Webrat
|
||||
class Area #:nodoc:
|
||||
class Area < Element #:nodoc:
|
||||
|
||||
def self.css_search
|
||||
"area"
|
||||
end
|
||||
|
||||
def initialize(session, element)
|
||||
@session = session
|
||||
@element = element
|
||||
def self.xpath_search
|
||||
".//area"
|
||||
end
|
||||
|
||||
def click(method = nil, options = {})
|
||||
|
22
lib/webrat/core/element.rb
Normal file
22
lib/webrat/core/element.rb
Normal file
@ -0,0 +1,22 @@
|
||||
module Webrat
|
||||
|
||||
class Element
|
||||
|
||||
def self.load_all(session, dom)
|
||||
Webrat::XML.xpath_search(dom, xpath_search).map do |element|
|
||||
load(session, element)
|
||||
end
|
||||
end
|
||||
|
||||
def self.load(session, element)
|
||||
session.elements[Webrat::XML.xpath_to(element)] ||= self.new(session, element)
|
||||
end
|
||||
|
||||
def initialize(session, element)
|
||||
@session = session
|
||||
@element = element
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
@ -2,21 +2,32 @@ require "cgi"
|
||||
require "webrat/core_extensions/blank"
|
||||
require "webrat/core_extensions/nil_to_param"
|
||||
|
||||
require "webrat/core/element"
|
||||
|
||||
module Webrat
|
||||
# Raised when Webrat is asked to manipulate a disabled form field
|
||||
class DisabledFieldError < WebratError
|
||||
end
|
||||
|
||||
class Field #:nodoc:
|
||||
class Field < Element #:nodoc:
|
||||
attr_reader :value
|
||||
|
||||
def self.xpath_search
|
||||
[".//button", ".//input", ".//textarea", ".//select"]
|
||||
end
|
||||
|
||||
def initialize(session, element)
|
||||
@session = session
|
||||
@element = element
|
||||
def self.field_classes
|
||||
@field_classes || []
|
||||
end
|
||||
|
||||
def self.inherited(klass)
|
||||
@field_classes ||= []
|
||||
@field_classes << klass
|
||||
# raise args.inspect
|
||||
end
|
||||
|
||||
def initialize(*args)
|
||||
super
|
||||
@value = default_value
|
||||
end
|
||||
|
||||
|
@ -1,18 +1,18 @@
|
||||
require "webrat/core/field"
|
||||
require "webrat/core_extensions/blank"
|
||||
|
||||
require "webrat/core/element"
|
||||
|
||||
module Webrat
|
||||
class Form #:nodoc:
|
||||
class Form < Element #:nodoc:
|
||||
attr_reader :element
|
||||
|
||||
def self.css_search
|
||||
"form"
|
||||
def self.xpath_search
|
||||
".//form"
|
||||
end
|
||||
|
||||
def initialize(session, element)
|
||||
@session = session
|
||||
@element = element
|
||||
@fields = nil
|
||||
def initialize(*args)
|
||||
super
|
||||
|
||||
fields # preload
|
||||
labels # preload
|
||||
|
@ -1,5 +1,7 @@
|
||||
require "webrat/core/element"
|
||||
|
||||
module Webrat
|
||||
class Label #:nodoc:
|
||||
class Label < Element #:nodoc:
|
||||
|
||||
attr_reader :element
|
||||
|
||||
@ -7,11 +9,6 @@ module Webrat
|
||||
".//label"
|
||||
end
|
||||
|
||||
def initialize(session, element)
|
||||
@session = session
|
||||
@element = element
|
||||
end
|
||||
|
||||
def text
|
||||
str = Webrat::XML.all_inner_text(@element)
|
||||
str.gsub!("\n","")
|
||||
|
@ -1,17 +1,18 @@
|
||||
require "webrat/core_extensions/blank"
|
||||
|
||||
require "webrat/core/element"
|
||||
|
||||
module Webrat
|
||||
class Link #:nodoc:
|
||||
class Link < Element #:nodoc:
|
||||
|
||||
def self.xpath_search
|
||||
".//a[@href]"
|
||||
end
|
||||
|
||||
def self.css_search
|
||||
"a[@href]"
|
||||
end
|
||||
|
||||
def initialize(session, element)
|
||||
@session = session
|
||||
@element = element
|
||||
end
|
||||
|
||||
def click(options = {})
|
||||
method = options[:method] || http_method
|
||||
return if href =~ /^#/ && method == :get
|
||||
|
@ -317,54 +317,25 @@ module Webrat
|
||||
end
|
||||
|
||||
def areas #:nodoc:
|
||||
Webrat::XML.css_search(dom, Area.css_search).map do |element|
|
||||
area = Area.new(@session, element)
|
||||
@session.elements[Webrat::XML.xpath_to(element)] = area
|
||||
area
|
||||
end
|
||||
@areas ||= Area.load_all(@session, dom)
|
||||
end
|
||||
|
||||
def links #:nodoc:
|
||||
Webrat::XML.css_search(dom, Link.css_search).map do |element|
|
||||
link = Link.new(@session, element)
|
||||
@session.elements[Webrat::XML.xpath_to(element)] = link
|
||||
link
|
||||
end
|
||||
@links ||= Link.load_all(@session, dom)
|
||||
end
|
||||
|
||||
def forms #:nodoc:
|
||||
return @forms if @forms
|
||||
|
||||
@forms = Webrat::XML.css_search(dom, Form.css_search).map do |element|
|
||||
form = Form.new(@session, element)
|
||||
@session.elements[Webrat::XML.xpath_to(element)] = form
|
||||
form
|
||||
end
|
||||
@forms ||= Form.load_all(@session, dom)
|
||||
end
|
||||
|
||||
def fields
|
||||
return @fields if @fields
|
||||
|
||||
@fields = []
|
||||
|
||||
[SelectField, TextareaField, ButtonField, CheckboxField, PasswordField,
|
||||
RadioField, FileField, ResetField, TextField, HiddenField].each do |field_class|
|
||||
@fields += Webrat::XML.xpath_search(dom, *field_class.xpath_search).map do |element|
|
||||
field = field_class.new(self, element)
|
||||
@session.elements[Webrat::XML.xpath_to(element)] = field
|
||||
field
|
||||
end
|
||||
end
|
||||
|
||||
@fields
|
||||
@fields ||= Field.field_classes.map do |field_class|
|
||||
field_class.load_all(@session, dom)
|
||||
end.flatten
|
||||
end
|
||||
|
||||
def labels
|
||||
@labels ||= Webrat::XML.css_search(dom, "label").map do |element|
|
||||
label = Label.new(self, element)
|
||||
@session.elements[Webrat::XML.xpath_to(element)] = label
|
||||
label
|
||||
end
|
||||
@labels ||= Label.load_all(@session, dom)
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user