Stop initializing Fields with references to the Form. They can find it by walking the DOM
This commit is contained in:
parent
565e5d221d
commit
9e743ea3e2
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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!(" ")
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue