Skip to content

Commit

Permalink
Looking up Fields using Session#elements hash
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 29, 2008
1 parent 8e864f7 commit e0488cd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
14 changes: 4 additions & 10 deletions lib/webrat/core/form.rb
Expand Up @@ -13,16 +13,8 @@ def initialize(session, element)
@session = session
@element = element
@fields = nil
end

def field_by_element(element, *field_types)
return nil if element.nil?

expected_path = Webrat::XML.xpath_to(element)

fields_by_type(field_types).detect do |possible_field|
possible_field.path == expected_path
end
fields # preload
end

def find_select_option(option_text)
Expand All @@ -44,7 +36,9 @@ def fields
[SelectField, TextareaField, ButtonField, CheckboxField, PasswordField,
RadioField, FileField, ResetField, TextField, HiddenField].each do |field_class|
@fields += Webrat::XML.xpath_search(@element, *field_class.xpath_search).map do |element|
field_class.new(self, element)
field = field_class.new(self, element)
@session.elements[Webrat::XML.xpath_to(element)] = field
field
end
end

Expand Down
11 changes: 3 additions & 8 deletions lib/webrat/core/locators.rb
Expand Up @@ -4,11 +4,7 @@ module Webrat
module Locators

def field_by_xpath(xpath)
element = Webrat::XML.xpath_search(dom, xpath).first

forms.detect_mapped do |form|
form.field_by_element(element)
end
field_by_element(Webrat::XML.xpath_at(dom, xpath))
end

def field(*args) # :nodoc:
Expand Down Expand Up @@ -47,9 +43,8 @@ def find_field_named(name, *field_types) #:nodoc:
end

def field_by_element(element, *field_types)
forms.detect_mapped do |form|
form.field_by_element(element, *field_types)
end
return nil if element.nil?
@session.elements[Webrat::XML.xpath_to(element)]
end

def area_by_element(element)
Expand Down
1 change: 1 addition & 0 deletions lib/webrat/core/scope.rb
Expand Up @@ -29,6 +29,7 @@ def initialize(session, &block) #:nodoc:
@session = session
instance_eval(&block) if block_given?

forms # preload
areas # preload
end

Expand Down
6 changes: 5 additions & 1 deletion lib/webrat/core/xml.rb
Expand Up @@ -78,7 +78,11 @@ def self.attribute(element, attribute_name)
else
element.attributes[attribute_name]
end
end
end

def self.xpath_at(*args)
xpath_search(*args).first
end

def self.xpath_search(element, *searches)
searches.flatten.map do |search|
Expand Down

0 comments on commit e0488cd

Please sign in to comment.