Refactoring node location
This commit is contained in:
parent
53539eda09
commit
d48a0fcb15
@ -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
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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?
|
||||||
|
@ -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.
|
||||||
|
Loading…
Reference in New Issue
Block a user