diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 28271d2..a06fbe5 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -14,10 +14,9 @@ module Webrat [".//button", ".//input", ".//textarea", ".//select"] end - def initialize(form, element) - @form = form + def initialize(session, element) + @session = session @element = element - @value = default_value end @@ -75,6 +74,19 @@ module Webrat protected + def form + @session.element_to_webrat_element(form_element) + end + + def form_element + parent = @element.parent + + while parent.respond_to?(:parent) + return parent if parent.name == 'form' + parent = parent.parent + end + end + def name Webrat::XML.attribute(@element, "name") end @@ -101,7 +113,7 @@ module Webrat end unless id.blank? - @label_elements += Webrat::XML.css_search(@form.element, "label[@for='#{id}']") + @label_elements += Webrat::XML.css_search(form.element, "label[@for='#{id}']") end @label_elements @@ -146,7 +158,7 @@ module Webrat def click raise_error_if_disabled set(Webrat::XML.attribute(@element, "value")) unless Webrat::XML.attribute(@element, "name").blank? - @form.submit + form.submit end end @@ -161,7 +173,7 @@ module Webrat if collection_name? super else - checkbox_with_same_name = @form.field_named(name, CheckboxField) + checkbox_with_same_name = form.field_named(name, CheckboxField) if checkbox_with_same_name.to_param.blank? super @@ -251,7 +263,7 @@ module Webrat protected def other_options - @form.fields.select { |f| f.name == name } + form.fields.select { |f| f.name == name } end def default_value diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 024adc4..c9963eb 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -37,7 +37,7 @@ module Webrat [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 = field_class.new(self, element) + field = field_class.new(@session, element) @session.elements[Webrat::XML.xpath_to(element)] = field field end diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index 2191b1a..11d1018 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -16,6 +16,7 @@ module Webrat 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 diff --git a/lib/webrat/core/locators/field_labeled_locator.rb b/lib/webrat/core/locators/field_labeled_locator.rb index 1447f35..ebf7af4 100644 --- a/lib/webrat/core/locators/field_labeled_locator.rb +++ b/lib/webrat/core/locators/field_labeled_locator.rb @@ -13,20 +13,38 @@ module Webrat matching_fields = possible_fields.select do |possible_field| possible_field.send(:labels).any? do |label| - text(label) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i + text(label.element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i end end matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length } end + + # matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length } + end + + def matching_fields + matching_labels.map(&:field).compact.uniq + end + + def matching_labels + matching_label_elements.map do |label_element| + @scope.element_to_webrat_element(label_element) + end + end + + def matching_label_elements + label_elements.select do |label_element| + text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i + end end def error_message "Could not find field labeled #{@value.inspect}" end - def text(label) - str = Webrat::XML.all_inner_text(label.element) + def text(element) + str = Webrat::XML.all_inner_text(element) str.gsub!("\n","") str.strip! str.squeeze!(" ") diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 2129e90..2af8fce 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -334,7 +334,9 @@ module Webrat return @forms if @forms @forms = Webrat::XML.css_search(dom, Form.css_search).map do |element| - Form.new(@session, element) + form = Form.new(@session, element) + @session.elements[Webrat::XML.xpath_to(element)] = form + form end end diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 1d12e48..803479c 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -43,6 +43,11 @@ 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.