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
|
module Webrat
|
||||||
class Area #:nodoc:
|
class Area < Element #:nodoc:
|
||||||
|
|
||||||
def self.css_search
|
def self.xpath_search
|
||||||
"area"
|
".//area"
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(session, element)
|
|
||||||
@session = session
|
|
||||||
@element = element
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def click(method = nil, options = {})
|
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,22 +2,33 @@ require "cgi"
|
|||||||
require "webrat/core_extensions/blank"
|
require "webrat/core_extensions/blank"
|
||||||
require "webrat/core_extensions/nil_to_param"
|
require "webrat/core_extensions/nil_to_param"
|
||||||
|
|
||||||
|
require "webrat/core/element"
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
# Raised when Webrat is asked to manipulate a disabled form field
|
# Raised when Webrat is asked to manipulate a disabled form field
|
||||||
class DisabledFieldError < WebratError
|
class DisabledFieldError < WebratError
|
||||||
end
|
end
|
||||||
|
|
||||||
class Field #:nodoc:
|
class Field < Element #:nodoc:
|
||||||
attr_reader :value
|
attr_reader :value
|
||||||
|
|
||||||
def self.xpath_search
|
def self.xpath_search
|
||||||
[".//button", ".//input", ".//textarea", ".//select"]
|
[".//button", ".//input", ".//textarea", ".//select"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(session, element)
|
def self.field_classes
|
||||||
@session = session
|
@field_classes || []
|
||||||
@element = element
|
end
|
||||||
@value = default_value
|
|
||||||
|
def self.inherited(klass)
|
||||||
|
@field_classes ||= []
|
||||||
|
@field_classes << klass
|
||||||
|
# raise args.inspect
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(*args)
|
||||||
|
super
|
||||||
|
@value = default_value
|
||||||
end
|
end
|
||||||
|
|
||||||
def label_text
|
def label_text
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
require "webrat/core/field"
|
require "webrat/core/field"
|
||||||
require "webrat/core_extensions/blank"
|
require "webrat/core_extensions/blank"
|
||||||
|
|
||||||
|
require "webrat/core/element"
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
class Form #:nodoc:
|
class Form < Element #:nodoc:
|
||||||
attr_reader :element
|
attr_reader :element
|
||||||
|
|
||||||
def self.css_search
|
def self.xpath_search
|
||||||
"form"
|
".//form"
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(session, element)
|
def initialize(*args)
|
||||||
@session = session
|
super
|
||||||
@element = element
|
|
||||||
@fields = nil
|
|
||||||
|
|
||||||
fields # preload
|
fields # preload
|
||||||
labels # preload
|
labels # preload
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
require "webrat/core/element"
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
class Label #:nodoc:
|
class Label < Element #:nodoc:
|
||||||
|
|
||||||
attr_reader :element
|
attr_reader :element
|
||||||
|
|
||||||
@ -7,11 +9,6 @@ module Webrat
|
|||||||
".//label"
|
".//label"
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(session, element)
|
|
||||||
@session = session
|
|
||||||
@element = element
|
|
||||||
end
|
|
||||||
|
|
||||||
def text
|
def text
|
||||||
str = Webrat::XML.all_inner_text(@element)
|
str = Webrat::XML.all_inner_text(@element)
|
||||||
str.gsub!("\n","")
|
str.gsub!("\n","")
|
||||||
|
@ -1,17 +1,18 @@
|
|||||||
require "webrat/core_extensions/blank"
|
require "webrat/core_extensions/blank"
|
||||||
|
|
||||||
|
require "webrat/core/element"
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
class Link #:nodoc:
|
class Link < Element #:nodoc:
|
||||||
|
|
||||||
|
def self.xpath_search
|
||||||
|
".//a[@href]"
|
||||||
|
end
|
||||||
|
|
||||||
def self.css_search
|
def self.css_search
|
||||||
"a[@href]"
|
"a[@href]"
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(session, element)
|
|
||||||
@session = session
|
|
||||||
@element = element
|
|
||||||
end
|
|
||||||
|
|
||||||
def click(options = {})
|
def click(options = {})
|
||||||
method = options[:method] || http_method
|
method = options[:method] || http_method
|
||||||
return if href =~ /^#/ && method == :get
|
return if href =~ /^#/ && method == :get
|
||||||
|
@ -317,54 +317,25 @@ module Webrat
|
|||||||
end
|
end
|
||||||
|
|
||||||
def areas #:nodoc:
|
def areas #:nodoc:
|
||||||
Webrat::XML.css_search(dom, Area.css_search).map do |element|
|
@areas ||= Area.load_all(@session, dom)
|
||||||
area = Area.new(@session, element)
|
|
||||||
@session.elements[Webrat::XML.xpath_to(element)] = area
|
|
||||||
area
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def links #:nodoc:
|
def links #:nodoc:
|
||||||
Webrat::XML.css_search(dom, Link.css_search).map do |element|
|
@links ||= Link.load_all(@session, dom)
|
||||||
link = Link.new(@session, element)
|
|
||||||
@session.elements[Webrat::XML.xpath_to(element)] = link
|
|
||||||
link
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def forms #:nodoc:
|
def forms #:nodoc:
|
||||||
return @forms if @forms
|
@forms ||= Form.load_all(@session, dom)
|
||||||
|
|
||||||
@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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fields
|
def fields
|
||||||
return @fields if @fields
|
@fields ||= Field.field_classes.map do |field_class|
|
||||||
|
field_class.load_all(@session, dom)
|
||||||
@fields = []
|
end.flatten
|
||||||
|
|
||||||
[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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def labels
|
def labels
|
||||||
@labels ||= Webrat::XML.css_search(dom, "label").map do |element|
|
@labels ||= Label.load_all(@session, dom)
|
||||||
label = Label.new(self, element)
|
|
||||||
@session.elements[Webrat::XML.xpath_to(element)] = label
|
|
||||||
label
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user