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



76
77
78
# File 'lib/webrat/core/locators/link_locator.rb', line 76

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| a.inner_text.length <=> b.inner_text.length }
end


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

def link_elements
  @dom.xpath(*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)


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

def matches_id?(link)
  if @value.is_a?(Regexp)
    link["id"] =~ @value ? true : false
  else
    link["id"] == @value ? true : false
  end
end

#matches_img?(link) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  link.css("img").any? do |img|
    img["title"] =~ matcher ||
    img["src"] =~ matcher
  end
end

#matches_text?(link) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
# File 'lib/webrat/core/locators/link_locator.rb', line 37

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

  replace_nbsp(link.inner_text) =~ matcher ||
  link["title"] =~ matcher
end


16
17
18
19
20
21
22
# 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) ||
    matches_img?(link_element)
  end
end

#replace_nbsp(str) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/webrat/core/locators/link_locator.rb', line 60

def replace_nbsp(str)
  if str.respond_to?(:valid_encoding?)
    if str.valid_encoding?
      str.gsub(/\xc2\xa0/u, ' ')
    else
      str.force_encoding('UTF-8').gsub(/\xc2\xa0/u, ' ')
    end
  else
    str.gsub(/\xc2\xa0/u, ' ')
  end
end

#replace_nbsp_ref(str) ⇒ Object



72
73
74
# File 'lib/webrat/core/locators/link_locator.rb', line 72

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