From f2758c5d812f823ca25a11598a9a1a224bda819b Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 28 Nov 2008 19:51:54 -0500 Subject: [PATCH] Search for areas using the DOM --- lib/webrat/core/area.rb | 20 +++++++------------- lib/webrat/core/locators.rb | 29 ++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/webrat/core/area.rb b/lib/webrat/core/area.rb index 63f4cd9..5c9dedf 100644 --- a/lib/webrat/core/area.rb +++ b/lib/webrat/core/area.rb @@ -10,25 +10,19 @@ module Webrat @session.request_page(absolute_href, :get, {}) end - def matches_text?(id_or_title) - matcher = /#{Regexp.escape(id_or_title.to_s)}/i - title =~ matcher || id =~ matcher + def path + if Webrat.configuration.parse_with_nokogiri? + @element.path + else + @element.xpath + end end - protected + protected def href Webrat::XML.attribute(@element, "href") end - - def title - Webrat::XML.attribute(@element, "title") - end - - def id - Webrat::XML.attribute(@element, "id") - end - def absolute_href if href =~ /^\?/ diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index 324586f..7e8a3be 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -52,6 +52,20 @@ module Webrat 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: field_elements = Webrat::XML.css_search(dom, "button", "input", "textarea", "select") @@ -102,9 +116,18 @@ module Webrat end end - def find_area(area_name) #:nodoc: - areas.detect { |area| area.matches_text?(area_name) } || - raise(NotFoundError.new("Could not find area with name #{area_name}")) + def find_area(id_or_title) #:nodoc: + area_elements = Webrat::XML.css_search(dom, "area") + + 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 def find_link(text_or_title_or_id) #:nodoc: