Skip to content

Commit

Permalink
Search for areas using the DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 29, 2008
1 parent 0b1dfbe commit f2758c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
20 changes: 7 additions & 13 deletions lib/webrat/core/area.rb
Expand Up @@ -10,25 +10,19 @@ def click(method = nil, options = {})
@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 =~ /^\?/
Expand Down
29 changes: 26 additions & 3 deletions lib/webrat/core/locators.rb
Expand Up @@ -52,6 +52,20 @@ def field_by_element(element, *field_types)
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")

Expand Down Expand Up @@ -102,9 +116,18 @@ def find_button(value) #:nodoc:
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:
Expand Down

0 comments on commit f2758c5

Please sign in to comment.