Refactoring node location
This commit is contained in:
parent
53539eda09
commit
d48a0fcb15
|
@ -9,6 +9,7 @@ module Webrat
|
|||
end
|
||||
|
||||
def self.load(session, element)
|
||||
return nil if element.nil?
|
||||
session.elements[Webrat::XML.xpath_to(element)] ||= self.new(session, element)
|
||||
end
|
||||
|
||||
|
|
|
@ -26,6 +26,31 @@ module Webrat
|
|||
# raise args.inspect
|
||||
end
|
||||
|
||||
def self.load(session, element)
|
||||
return nil if element.nil?
|
||||
session.elements[Webrat::XML.xpath_to(element)] ||= field_class(element).new(session, element)
|
||||
end
|
||||
|
||||
def self.field_class(element)
|
||||
case element.name
|
||||
when "button" then ButtonField
|
||||
when "select" then SelectField
|
||||
when "textarea" then TextareaField
|
||||
else
|
||||
case Webrat::XML.attribute(element, "type")
|
||||
when "checkbox" then CheckboxField
|
||||
when "hidden" then HiddenField
|
||||
when "radio" then RadioField
|
||||
when "password" then PasswordField
|
||||
when "file" then FileField
|
||||
when "reset" then ResetField
|
||||
when "submit" then ButtonField
|
||||
when "image" then ButtonField
|
||||
else TextField
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(*args)
|
||||
super
|
||||
@value = default_value
|
||||
|
@ -82,7 +107,7 @@ module Webrat
|
|||
protected
|
||||
|
||||
def form
|
||||
@session.element_to_webrat_element(form_element)
|
||||
Form.load(@session, form_element)
|
||||
end
|
||||
|
||||
def form_element
|
||||
|
|
|
@ -23,7 +23,7 @@ module Webrat
|
|||
protected
|
||||
|
||||
def select
|
||||
@session.element_to_webrat_element(select_element)
|
||||
SelectField.load(@session, select_element)
|
||||
end
|
||||
|
||||
def select_element
|
||||
|
|
|
@ -12,13 +12,7 @@ module Webrat
|
|||
module Locators
|
||||
|
||||
def field_by_xpath(xpath)
|
||||
element_to_webrat_element(Webrat::XML.xpath_at(dom, xpath))
|
||||
end
|
||||
|
||||
def element_to_webrat_element(element)
|
||||
@session.element_to_webrat_element(element)
|
||||
return nil if element.nil?
|
||||
@session.elements[Webrat::XML.xpath_to(element)]
|
||||
Field.load(@session, Webrat::XML.xpath_at(dom, xpath))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -6,7 +6,7 @@ module Webrat
|
|||
class AreaLocator < Locator
|
||||
|
||||
def locate
|
||||
@scope.element_to_webrat_element(area_element)
|
||||
Area.load(@scope.session, area_element)
|
||||
end
|
||||
|
||||
def area_element
|
||||
|
|
|
@ -6,7 +6,7 @@ module Webrat
|
|||
class ButtonLocator < Locator
|
||||
|
||||
def locate
|
||||
@scope.element_to_webrat_element(button_element)
|
||||
ButtonField.load(@scope.session, button_element)
|
||||
end
|
||||
|
||||
def button_element
|
||||
|
|
|
@ -6,7 +6,7 @@ module Webrat
|
|||
class FieldByIdLocator < Locator
|
||||
|
||||
def locate
|
||||
@scope.element_to_webrat_element(field_element)
|
||||
Field.load(@scope.session, field_element)
|
||||
end
|
||||
|
||||
def field_element
|
||||
|
|
|
@ -29,7 +29,7 @@ module Webrat
|
|||
|
||||
def matching_labels
|
||||
matching_label_elements.map do |label_element|
|
||||
@scope.element_to_webrat_element(label_element)
|
||||
Label.load(@scope.session, label_element)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ module Webrat
|
|||
class FieldNamedLocator < Locator
|
||||
|
||||
def locate
|
||||
@scope.element_to_webrat_element(field_element)
|
||||
Field.load(@scope.session, field_element)
|
||||
end
|
||||
|
||||
def field_element
|
||||
|
|
|
@ -7,7 +7,7 @@ module Webrat
|
|||
class LabelLocator < Locator
|
||||
|
||||
def locate
|
||||
@scope.element_to_webrat_element(label_element)
|
||||
Label.load(@scope.session, label_element)
|
||||
end
|
||||
|
||||
def label_element
|
||||
|
|
|
@ -6,7 +6,7 @@ module Webrat
|
|||
class LinkLocator < Locator
|
||||
|
||||
def locate
|
||||
@scope.element_to_webrat_element(link_element)
|
||||
Link.load(@scope.session, link_element)
|
||||
end
|
||||
|
||||
def link_element
|
||||
|
|
|
@ -25,6 +25,8 @@ module Webrat
|
|||
end
|
||||
end
|
||||
|
||||
attr_reader :session
|
||||
|
||||
def initialize(session, &block) #:nodoc:
|
||||
@session = session
|
||||
instance_eval(&block) if block_given?
|
||||
|
|
|
@ -43,11 +43,6 @@ module Webrat
|
|||
|
||||
reset
|
||||
end
|
||||
|
||||
def element_to_webrat_element(element)
|
||||
return nil if element.nil?
|
||||
elements[Webrat::XML.xpath_to(element)]
|
||||
end
|
||||
|
||||
# Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
|
||||
# web browser if on OS X. Useful for debugging.
|
||||
|
|
Loading…
Reference in New Issue