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
def self.load(session, element)
return nil if element.nil?
session.elements[Webrat::XML.xpath_to(element)] ||= self.new(session, element)
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

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

View File

@ -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.