Initialize locators with a session and a dom instead of a scope

This commit is contained in:
Bryan Helmkamp 2008-11-30 15:47:31 -05:00
parent 9ace546766
commit 522bb3272f
12 changed files with 43 additions and 41 deletions

View File

@ -6,7 +6,7 @@ module Webrat
class AreaLocator < Locator class AreaLocator < Locator
def locate def locate
Area.load(@scope.session, area_element) Area.load(@session, area_element)
end end
def area_element def area_element
@ -21,7 +21,7 @@ module Webrat
end end
def area_elements def area_elements
Webrat::XML.xpath_search(@scope.dom, Area.xpath_search) Webrat::XML.xpath_search(@dom, Area.xpath_search)
end end
def error_message def error_message
@ -31,7 +31,7 @@ module Webrat
end end
def find_area(id_or_title) #:nodoc: def find_area(id_or_title) #:nodoc:
AreaLocator.new(self, id_or_title).locate! AreaLocator.new(@session, dom, id_or_title).locate!
end end
end end

View File

@ -6,7 +6,7 @@ module Webrat
class ButtonLocator < Locator class ButtonLocator < Locator
def locate def locate
ButtonField.load(@scope.session, button_element) ButtonField.load(@session, button_element)
end end
def button_element def button_element
@ -37,7 +37,7 @@ module Webrat
end end
def button_elements def button_elements
Webrat::XML.xpath_search(@scope.dom, *ButtonField.xpath_search) Webrat::XML.xpath_search(@dom, *ButtonField.xpath_search)
end end
def error_message def error_message
@ -47,7 +47,7 @@ module Webrat
end end
def find_button(value) #:nodoc: def find_button(value) #:nodoc:
ButtonLocator.new(self, value).locate! ButtonLocator.new(@session, dom, value).locate!
end end
end end

View File

@ -6,7 +6,7 @@ module Webrat
class FieldByIdLocator < Locator class FieldByIdLocator < Locator
def locate def locate
Field.load(@scope.session, field_element) Field.load(@session, field_element)
end end
def field_element def field_element
@ -20,7 +20,7 @@ module Webrat
end end
def field_elements def field_elements
Webrat::XML.xpath_search(@scope.dom, *Field.xpath_search) Webrat::XML.xpath_search(@dom, *Field.xpath_search)
end end
def error_message def error_message
@ -30,7 +30,7 @@ module Webrat
end end
def field_with_id(id, *field_types) def field_with_id(id, *field_types)
FieldByIdLocator.new(self, id, *field_types).locate! FieldByIdLocator.new(@session, dom, id, *field_types).locate!
end end
end end

View File

@ -14,7 +14,7 @@ module Webrat
matching_label_elements.sort_by do |label_element| matching_label_elements.sort_by do |label_element|
text(label_element).length text(label_element).length
end.map do |label_element| end.map do |label_element|
Label.load(@scope.session, label_element) Label.load(@session, label_element)
end end
end end
@ -25,7 +25,7 @@ module Webrat
end end
def label_elements def label_elements
Webrat::XML.xpath_search(@scope.dom, Label.xpath_search) Webrat::XML.xpath_search(@dom, Label.xpath_search)
end end
def error_message def error_message
@ -43,7 +43,7 @@ module Webrat
end end
def field_labeled(label, *field_types) def field_labeled(label, *field_types)
FieldLabeledLocator.new(self, label, *field_types).locate! FieldLabeledLocator.new(@session, dom, label, *field_types).locate!
end end
end end

View File

@ -6,9 +6,9 @@ module Webrat
class FieldLocator < Locator class FieldLocator < Locator
def locate def locate
FieldByIdLocator.new(@scope, @value).locate || FieldByIdLocator.new(@session, @dom, @value).locate ||
FieldNamedLocator.new(@scope, @value, *@field_types).locate || FieldNamedLocator.new(@session, @dom, @value, *@field_types).locate ||
FieldLabeledLocator.new(@scope, @value, *@field_types).locate FieldLabeledLocator.new(@session, @dom, @value, *@field_types).locate
end end
def error_message def error_message
@ -18,7 +18,7 @@ module Webrat
end end
def field(*args) # :nodoc: def field(*args) # :nodoc:
FieldLocator.new(self, *args).locate! FieldLocator.new(@session, dom, *args).locate!
end end
end end

View File

@ -6,7 +6,7 @@ module Webrat
class FieldNamedLocator < Locator class FieldNamedLocator < Locator
def locate def locate
Field.load(@scope.session, field_element) Field.load(@session, field_element)
end end
def field_element def field_element
@ -16,7 +16,7 @@ module Webrat
end end
def field_elements def field_elements
Webrat::XML.xpath_search(@scope.dom, *xpath_searches) Webrat::XML.xpath_search(@dom, *xpath_searches)
end end
def xpath_searches def xpath_searches
@ -34,7 +34,7 @@ module Webrat
end end
def field_named(name, *field_types) def field_named(name, *field_types)
FieldNamedLocator.new(self, name, *field_types).locate! FieldNamedLocator.new(@session, dom, name, *field_types).locate!
end end
end end

View File

@ -6,11 +6,11 @@ module Webrat
class FormLocator < Locator class FormLocator < Locator
def locate def locate
Form.load(@scope.session, form_element) Form.load(@session, form_element)
end end
def form_element def form_element
Webrat::XML.css_at(@scope.dom, "#" + @value) Webrat::XML.css_at(@dom, "#" + @value)
end end
end end

View File

@ -7,7 +7,7 @@ module Webrat
class LabelLocator < Locator class LabelLocator < Locator
def locate def locate
Label.load(@scope.session, label_element) Label.load(@session, label_element)
end end
def label_element def label_element
@ -17,7 +17,7 @@ module Webrat
end end
def label_elements def label_elements
Webrat::XML.xpath_search(@scope.dom, Label.xpath_search) Webrat::XML.xpath_search(@dom, Label.xpath_search)
end end
def text(label_element) def text(label_element)

View File

@ -6,7 +6,7 @@ module Webrat
class LinkLocator < Locator class LinkLocator < Locator
def locate def locate
Link.load(@scope.session, link_element) Link.load(@session, link_element)
end end
def link_element def link_element
@ -41,7 +41,7 @@ module Webrat
end end
def link_elements def link_elements
Webrat::XML.css_search(@scope.dom, *Link.css_search) Webrat::XML.css_search(@dom, *Link.css_search)
end end
def replace_nbsp(str) def replace_nbsp(str)
@ -59,7 +59,7 @@ module Webrat
end end
def find_link(text_or_title_or_id) #:nodoc: def find_link(text_or_title_or_id) #:nodoc:
LinkLocator.new(self, text_or_title_or_id).locate! LinkLocator.new(@session, dom, text_or_title_or_id).locate!
end end
end end

View File

@ -3,8 +3,9 @@ module Webrat
class Locator class Locator
def initialize(scope, value, *field_types) def initialize(session, dom, value, *field_types)
@scope = scope @session = session
@dom = dom
@value = value @value = value
@field_types = field_types @field_types = field_types
end end

View File

@ -6,15 +6,16 @@ module Webrat
class SelectOptionLocator < Locator class SelectOptionLocator < Locator
def initialize(scope, option_text, id_or_name_or_label) def initialize(session, dom, option_text, id_or_name_or_label)
@scope = scope @session = session
@dom = dom
@option_text = option_text @option_text = option_text
@id_or_name_or_label = id_or_name_or_label @id_or_name_or_label = id_or_name_or_label
end end
def locate def locate
if @id_or_name_or_label if @id_or_name_or_label
field = FieldLocator.new(@scope, @id_or_name_or_label, SelectField).locate! field = FieldLocator.new(@session, @dom, @id_or_name_or_label, SelectField).locate!
field.send(:options).detect do |o| field.send(:options).detect do |o|
if @option_text.is_a?(Regexp) if @option_text.is_a?(Regexp)
@ -32,12 +33,12 @@ module Webrat
end end
end end
SelectOption.load(@scope.session, option_element) SelectOption.load(@session, option_element)
end end
end end
def option_elements def option_elements
Webrat::XML.xpath_search(@scope.dom, *SelectOption.xpath_search) Webrat::XML.xpath_search(@dom, *SelectOption.xpath_search)
end end
def error_message def error_message
@ -51,7 +52,7 @@ module Webrat
end end
def find_select_option(option_text, id_or_name_or_label) #:nodoc: def find_select_option(option_text, id_or_name_or_label) #:nodoc:
SelectOptionLocator.new(self, option_text, id_or_name_or_label).locate! SelectOptionLocator.new(@session, dom, option_text, id_or_name_or_label).locate!
end end
end end

View File

@ -132,7 +132,7 @@ module Webrat
date_to_select : Date.parse(date_to_select) date_to_select : Date.parse(date_to_select)
id_prefix = locate_id_prefix(options) do id_prefix = locate_id_prefix(options) do
year_field = FieldByIdLocator.new(self, /(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/).locate year_field = FieldByIdLocator.new(@session, dom, /(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/).locate
raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/ raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/
$1 $1
end end
@ -166,7 +166,7 @@ module Webrat
time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select) time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select)
id_prefix = locate_id_prefix(options) do id_prefix = locate_id_prefix(options) do
hour_field = FieldByIdLocator.new(self, /(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/).locate hour_field = FieldByIdLocator.new(@session, dom, /(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/).locate
raise NotFoundError.new("No time fields were found") unless hour_field && hour_field.id =~ /(.*?)_4i/ raise NotFoundError.new("No time fields were found") unless hour_field && hour_field.id =~ /(.*?)_4i/
$1 $1
end end
@ -188,7 +188,7 @@ module Webrat
def select_datetime(time_to_select, options ={}) def select_datetime(time_to_select, options ={})
time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select) time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select)
options[:id_prefix] ||= (options[:from] ? FieldByIdLocator.new(self, options[:from]).locate : nil) options[:id_prefix] ||= (options[:from] ? FieldByIdLocator.new(@session, dom, options[:from]).locate : nil)
select_date time, options select_date time, options
select_time time, options select_time time, options
@ -261,7 +261,7 @@ module Webrat
webrat_deprecate :clicks_button, :click_button webrat_deprecate :clicks_button, :click_button
def submit_form(id) def submit_form(id)
FormLocator.new(self, id).locate.submit FormLocator.new(@session, dom, id).locate.submit
end end
def dom # :nodoc: def dom # :nodoc:
@ -301,7 +301,7 @@ module Webrat
return options[:id_prefix] if options[:id_prefix] return options[:id_prefix] if options[:id_prefix]
if options[:from] if options[:from]
if (label = LabelLocator.new(self, options[:from]).locate) if (label = LabelLocator.new(@session, dom, options[:from]).locate)
label.for_id label.for_id
else else
raise NotFoundError.new("Could not find the label with text #{options[:from]}") raise NotFoundError.new("Could not find the label with text #{options[:from]}")