Skip to content

Commit

Permalink
Refactoring node location
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 30, 2008
1 parent 53539ed commit d48a0fc
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/webrat/core/elements/element.rb
Expand Up @@ -9,6 +9,7 @@ def self.load_all(session, dom)
end

def self.load(session, element)
return nil if element.nil?
session.elements[Webrat::XML.xpath_to(element)] ||= self.new(session, element)
end

Expand Down
27 changes: 26 additions & 1 deletion lib/webrat/core/elements/field.rb
Expand Up @@ -26,6 +26,31 @@ def self.inherited(klass)
# 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
Expand Down Expand Up @@ -82,7 +107,7 @@ def unset
protected

def form
@session.element_to_webrat_element(form_element)
Form.load(@session, form_element)
end

def form_element
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/elements/select_option.rb
Expand Up @@ -23,7 +23,7 @@ def choose
protected

def select
@session.element_to_webrat_element(select_element)
SelectField.load(@session, select_element)
end

def select_element
Expand Down
8 changes: 1 addition & 7 deletions lib/webrat/core/locators.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/locators/area_locator.rb
Expand Up @@ -6,7 +6,7 @@ module Locators
class AreaLocator < Locator

def locate
@scope.element_to_webrat_element(area_element)
Area.load(@scope.session, area_element)
end

def area_element
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/locators/button_locator.rb
Expand Up @@ -6,7 +6,7 @@ module Locators
class ButtonLocator < Locator

def locate
@scope.element_to_webrat_element(button_element)
ButtonField.load(@scope.session, button_element)
end

def button_element
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/locators/field_by_id_locator.rb
Expand Up @@ -6,7 +6,7 @@ module Locators
class FieldByIdLocator < Locator

def locate
@scope.element_to_webrat_element(field_element)
Field.load(@scope.session, field_element)
end

def field_element
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/locators/field_labeled_locator.rb
Expand Up @@ -29,7 +29,7 @@ def matching_fields

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

Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/locators/field_named_locator.rb
Expand Up @@ -6,7 +6,7 @@ module Locators
class FieldNamedLocator < Locator

def locate
@scope.element_to_webrat_element(field_element)
Field.load(@scope.session, field_element)
end

def field_element
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/locators/label_locator.rb
Expand Up @@ -7,7 +7,7 @@ module Locators
class LabelLocator < Locator

def locate
@scope.element_to_webrat_element(label_element)
Label.load(@scope.session, label_element)
end

def label_element
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/locators/link_locator.rb
Expand Up @@ -6,7 +6,7 @@ module Locators
class LinkLocator < Locator

def locate
@scope.element_to_webrat_element(link_element)
Link.load(@scope.session, link_element)
end

def link_element
Expand Down
2 changes: 2 additions & 0 deletions lib/webrat/core/scope.rb
Expand Up @@ -25,6 +25,8 @@ def self.from_scope(session, scope, selector) #:nodoc:
end
end

attr_reader :session

def initialize(session, &block) #:nodoc:
@session = session
instance_eval(&block) if block_given?
Expand Down
5 changes: 0 additions & 5 deletions lib/webrat/core/session.rb
Expand Up @@ -43,11 +43,6 @@ def initialize(context = nil) #:nodoc:

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.
Expand Down

0 comments on commit d48a0fc

Please sign in to comment.