Extract locators into a separate file
This commit is contained in:
parent
6bd43d11ed
commit
39e1b98897
56
lib/webrat/core/locators.rb
Normal file
56
lib/webrat/core/locators.rb
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
module Webrat
|
||||||
|
module Locators
|
||||||
|
|
||||||
|
def field_labeled(label)
|
||||||
|
find_field(label, TextField, TextareaField, CheckboxField, RadioField, HiddenField)
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_select_option(option_text, id_or_name_or_label)
|
||||||
|
if id_or_name_or_label
|
||||||
|
field = find_field(id_or_name_or_label, SelectField)
|
||||||
|
return field.find_option(option_text)
|
||||||
|
else
|
||||||
|
forms.each do |form|
|
||||||
|
result = form.find_select_option(option_text)
|
||||||
|
return result if result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
flunk("Could not find option #{option_text.inspect}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_button(value)
|
||||||
|
forms.each do |form|
|
||||||
|
button = form.find_button(value)
|
||||||
|
return button if button
|
||||||
|
end
|
||||||
|
flunk("Could not find button #{value.inspect}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_area(area_name)
|
||||||
|
areas.select{|area| area.matches_text?(area_name)}.first || flunk("Could not find area with name #{area_name}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_link(text, selector = nil)
|
||||||
|
matching_links = links_within(selector).select do |possible_link|
|
||||||
|
possible_link.matches_text?(text)
|
||||||
|
end
|
||||||
|
|
||||||
|
if matching_links.any?
|
||||||
|
matching_links.min { |a, b| a.text.length <=> b.text.length }
|
||||||
|
else
|
||||||
|
flunk("Could not find link with text #{text.inspect}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_field(id_or_name_or_label, *field_types)
|
||||||
|
forms.each do |form|
|
||||||
|
result = form.find_field(id_or_name_or_label, *field_types)
|
||||||
|
return result if result
|
||||||
|
end
|
||||||
|
|
||||||
|
flunk("Could not find #{field_types.inspect}: #{id_or_name_or_label.inspect}")
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
@ -1,12 +1,14 @@
|
|||||||
require "hpricot"
|
require "hpricot"
|
||||||
require "webrat/core/form"
|
require "webrat/core/form"
|
||||||
require "webrat/core/assertions"
|
require "webrat/core/assertions"
|
||||||
|
require "webrat/core/locators"
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
class Scope
|
class Scope
|
||||||
include Logging
|
include Logging
|
||||||
include Flunk
|
include Flunk
|
||||||
include Assertions
|
include Assertions
|
||||||
|
include Locators
|
||||||
|
|
||||||
def initialize(session, html, selector = nil)
|
def initialize(session, html, selector = nil)
|
||||||
@session = session
|
@session = session
|
||||||
@ -142,10 +144,6 @@ module Webrat
|
|||||||
@dom ||= Hpricot(scoped_html)
|
@dom ||= Hpricot(scoped_html)
|
||||||
end
|
end
|
||||||
|
|
||||||
def field_labeled(label)
|
|
||||||
find_field(label, TextField, TextareaField, CheckboxField, RadioField, HiddenField)
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def scoped_html
|
def scoped_html
|
||||||
@ -158,59 +156,12 @@ module Webrat
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_select_option(option_text, id_or_name_or_label)
|
|
||||||
if id_or_name_or_label
|
|
||||||
field = find_field(id_or_name_or_label, SelectField)
|
|
||||||
return field.find_option(option_text)
|
|
||||||
else
|
|
||||||
forms.each do |form|
|
|
||||||
result = form.find_select_option(option_text)
|
|
||||||
return result if result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
flunk("Could not find option #{option_text.inspect}")
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_button(value)
|
|
||||||
forms.each do |form|
|
|
||||||
button = form.find_button(value)
|
|
||||||
return button if button
|
|
||||||
end
|
|
||||||
flunk("Could not find button #{value.inspect}")
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_area(area_name)
|
|
||||||
areas.select{|area| area.matches_text?(area_name)}.first || flunk("Could not find area with name #{area_name}")
|
|
||||||
end
|
|
||||||
|
|
||||||
def areas
|
def areas
|
||||||
(dom / "area").map do |element|
|
(dom / "area").map do |element|
|
||||||
Area.new(@session, element)
|
Area.new(@session, element)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_link(text, selector = nil)
|
|
||||||
matching_links = links_within(selector).select do |possible_link|
|
|
||||||
possible_link.matches_text?(text)
|
|
||||||
end
|
|
||||||
|
|
||||||
if matching_links.any?
|
|
||||||
matching_links.min { |a, b| a.text.length <=> b.text.length }
|
|
||||||
else
|
|
||||||
flunk("Could not find link with text #{text.inspect}")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_field(id_or_name_or_label, *field_types)
|
|
||||||
forms.each do |form|
|
|
||||||
result = form.find_field(id_or_name_or_label, *field_types)
|
|
||||||
return result if result
|
|
||||||
end
|
|
||||||
|
|
||||||
flunk("Could not find #{field_types.inspect}: #{id_or_name_or_label.inspect}")
|
|
||||||
end
|
|
||||||
|
|
||||||
def links_within(selector)
|
def links_within(selector)
|
||||||
(dom / selector / "a[@href]").map do |link_element|
|
(dom / selector / "a[@href]").map do |link_element|
|
||||||
Link.new(@session, link_element)
|
Link.new(@session, link_element)
|
||||||
|
Loading…
Reference in New Issue
Block a user