Class: Webrat::Locators::LinkLocator

Inherits:
Locator show all
Defined in:
lib/webrat/core/locators/link_locator.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from Locator

#initialize, #locate!

Constructor Details

This class inherits a constructor from Webrat::Locators::Locator

Instance Method Details

#error_messageObject



55
56
57
# File 'lib/webrat/core/locators/link_locator.rb', line 55

def error_message
  "Could not find link with text or title or id #{@value.inspect}"
end


12
13
14
# File 'lib/webrat/core/locators/link_locator.rb', line 12

def link_element
  matching_links.min { |a, b| Webrat::XML.all_inner_text(a).length <=> Webrat::XML.all_inner_text(b).length }
end


43
44
45
# File 'lib/webrat/core/locators/link_locator.rb', line 43

def link_elements
  Webrat::XML.xpath_search(@dom, *Link.xpath_search)
end

#locateObject



8
9
10
# File 'lib/webrat/core/locators/link_locator.rb', line 8

def locate
  Link.load(@session, link_element)
end

#matches_id?(link) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/webrat/core/locators/link_locator.rb', line 35

def matches_id?(link)
  if @value.is_a?(Regexp)
    (Webrat::XML.attribute(link, "id") =~ @value) ? true : false
  else
    (Webrat::XML.attribute(link, "id") == @value) ? true : false
  end
end

#matches_text?(link) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/webrat/core/locators/link_locator.rb', line 23

def matches_text?(link)
  if @value.is_a?(Regexp)
    matcher = @value
  else
    matcher = /#{Regexp.escape(@value.to_s)}/i
  end

  replace_nbsp(Webrat::XML.all_inner_text(link)) =~ matcher ||
  replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher ||
  Webrat::XML.attribute(link, "title")=~ matcher
end


16
17
18
19
20
21
# File 'lib/webrat/core/locators/link_locator.rb', line 16

def matching_links
  @matching_links ||= link_elements.select do |link_element|
    matches_text?(link_element) ||
    matches_id?(link_element)
  end
end

#replace_nbsp(str) ⇒ Object



47
48
49
# File 'lib/webrat/core/locators/link_locator.rb', line 47

def replace_nbsp(str)
  str.gsub([0xA0].pack('U'), ' ')
end

#replace_nbsp_ref(str) ⇒ Object



51
52
53
# File 'lib/webrat/core/locators/link_locator.rb', line 51

def replace_nbsp_ref(str)
  str.gsub('&#xA0;',' ').gsub('&nbsp;', ' ')
end