Search for areas using the DOM

This commit is contained in:
Bryan Helmkamp 2008-11-28 19:51:54 -05:00
parent 0b1dfbe0a2
commit f2758c5d81
2 changed files with 33 additions and 16 deletions

View File

@ -10,25 +10,19 @@ module Webrat
@session.request_page(absolute_href, :get, {}) @session.request_page(absolute_href, :get, {})
end end
def matches_text?(id_or_title) def path
matcher = /#{Regexp.escape(id_or_title.to_s)}/i if Webrat.configuration.parse_with_nokogiri?
title =~ matcher || id =~ matcher @element.path
else
@element.xpath
end
end end
protected protected
def href def href
Webrat::XML.attribute(@element, "href") Webrat::XML.attribute(@element, "href")
end end
def title
Webrat::XML.attribute(@element, "title")
end
def id
Webrat::XML.attribute(@element, "id")
end
def absolute_href def absolute_href
if href =~ /^\?/ if href =~ /^\?/

View File

@ -52,6 +52,20 @@ module Webrat
end end
end end
def area_by_element(element)
return nil if element.nil?
if Webrat.configuration.parse_with_nokogiri?
expected_path = element.path
else
expected_path = element.xpath
end
areas.detect do |possible_area|
possible_area.path == expected_path
end
end
def find_field_with_id(id, *field_types) #:nodoc: def find_field_with_id(id, *field_types) #:nodoc:
field_elements = Webrat::XML.css_search(dom, "button", "input", "textarea", "select") field_elements = Webrat::XML.css_search(dom, "button", "input", "textarea", "select")
@ -102,9 +116,18 @@ module Webrat
end end
end end
def find_area(area_name) #:nodoc: def find_area(id_or_title) #:nodoc:
areas.detect { |area| area.matches_text?(area_name) } || area_elements = Webrat::XML.css_search(dom, "area")
raise(NotFoundError.new("Could not find area with name #{area_name}"))
matcher = /#{Regexp.escape(id_or_title.to_s)}/i
area_element = area_elements.detect do |area_element|
Webrat::XML.attribute(area_element, "title") =~ matcher ||
Webrat::XML.attribute(area_element, "id") =~ matcher
end
area_by_element(area_element) ||
raise(NotFoundError.new("Could not find area with name #{id_or_title}"))
end end
def find_link(text_or_title_or_id) #:nodoc: def find_link(text_or_title_or_id) #:nodoc: