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



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

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


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

def link_element
  matching_links.min { |a, b|
    a_score = a.inner_text =~ matcher ? a.inner_text.length : 100
    b_score = b.inner_text =~ matcher ? b.inner_text.length : 100
    a_score <=> b_score
  }
end


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

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

#matcherObject



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

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

#matches_id?(link) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#matches_text?(link) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/webrat/core/locators/link_locator.rb', line 27

def matches_text?(link)
  replace_nbsp(link.inner_text) =~ matcher ||
  replace_nbsp_ref(link.inner_html) =~ matcher ||
  link["title"] =~ matcher
end


20
21
22
23
24
25
# File 'lib/webrat/core/locators/link_locator.rb', line 20

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



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/webrat/core/locators/link_locator.rb', line 55

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



67
68
69
# File 'lib/webrat/core/locators/link_locator.rb', line 67

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