Refactoring node location

This commit is contained in:
Bryan Helmkamp 2008-11-30 14:46:52 -05:00
parent 53539eda09
commit d48a0fcb15
13 changed files with 38 additions and 21 deletions

View File

@ -9,6 +9,7 @@ module Webrat
end end
def self.load(session, element) def self.load(session, element)
return nil if element.nil?
session.elements[Webrat::XML.xpath_to(element)] ||= self.new(session, element) session.elements[Webrat::XML.xpath_to(element)] ||= self.new(session, element)
end end

View File

@ -26,6 +26,31 @@ module Webrat
# raise args.inspect # raise args.inspect
end 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) def initialize(*args)
super super
@value = default_value @value = default_value
@ -82,7 +107,7 @@ module Webrat
protected protected
def form def form
@session.element_to_webrat_element(form_element) Form.load(@session, form_element)
end end
def form_element def form_element

View File

@ -23,7 +23,7 @@ module Webrat
protected protected
def select def select
@session.element_to_webrat_element(select_element) SelectField.load(@session, select_element)
end end
def select_element def select_element

View File

@ -12,13 +12,7 @@ module Webrat
module Locators module Locators
def field_by_xpath(xpath) def field_by_xpath(xpath)
element_to_webrat_element(Webrat::XML.xpath_at(dom, xpath)) Field.load(@session, 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)]
end end
end end

View File

@ -6,7 +6,7 @@ module Webrat
class AreaLocator < Locator class AreaLocator < Locator
def locate def locate
@scope.element_to_webrat_element(area_element) Area.load(@scope.session, area_element)
end end
def area_element def area_element

View File

@ -6,7 +6,7 @@ module Webrat
class ButtonLocator < Locator class ButtonLocator < Locator
def locate def locate
@scope.element_to_webrat_element(button_element) ButtonField.load(@scope.session, button_element)
end end
def button_element def button_element

View File

@ -6,7 +6,7 @@ module Webrat
class FieldByIdLocator < Locator class FieldByIdLocator < Locator
def locate def locate
@scope.element_to_webrat_element(field_element) Field.load(@scope.session, field_element)
end end
def field_element def field_element

View File

@ -29,7 +29,7 @@ module Webrat
def matching_labels def matching_labels
matching_label_elements.map do |label_element| matching_label_elements.map do |label_element|
@scope.element_to_webrat_element(label_element) Label.load(@scope.session, label_element)
end end
end end

View File

@ -6,7 +6,7 @@ module Webrat
class FieldNamedLocator < Locator class FieldNamedLocator < Locator
def locate def locate
@scope.element_to_webrat_element(field_element) Field.load(@scope.session, field_element)
end end
def field_element def field_element

View File

@ -7,7 +7,7 @@ module Webrat
class LabelLocator < Locator class LabelLocator < Locator
def locate def locate
@scope.element_to_webrat_element(label_element) Label.load(@scope.session, label_element)
end end
def label_element def label_element

View File

@ -6,7 +6,7 @@ module Webrat
class LinkLocator < Locator class LinkLocator < Locator
def locate def locate
@scope.element_to_webrat_element(link_element) Link.load(@scope.session, link_element)
end end
def link_element def link_element

View File

@ -25,6 +25,8 @@ module Webrat
end end
end end
attr_reader :session
def initialize(session, &block) #:nodoc: def initialize(session, &block) #:nodoc:
@session = session @session = session
instance_eval(&block) if block_given? instance_eval(&block) if block_given?

View File

@ -43,11 +43,6 @@ module Webrat
reset reset
end 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 # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
# web browser if on OS X. Useful for debugging. # web browser if on OS X. Useful for debugging.