Class: Webrat::Matchers::HaveXpath

Inherits:
Object
  • Object
show all
Defined in:
lib/webrat/core/matchers/have_xpath.rb

Overview

:nodoc:

Direct Known Subclasses

HaveSelector

Instance Method Summary collapse

Constructor Details

#initialize(expected, options = {}, &block) ⇒ HaveXpath

Returns a new instance of HaveXpath.



7
8
9
10
11
# File 'lib/webrat/core/matchers/have_xpath.rb', line 7

def initialize(expected, options = {}, &block)
  @expected = expected
  @options  = options
  @block    = block
end

Instance Method Details

#add_attributes_conditions_to(query) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/webrat/core/matchers/have_xpath.rb', line 46

def add_attributes_conditions_to(query)
  attribute_conditions = []

  @options.each do |key, value|
    next if [:content, :count].include?(key)
    attribute_conditions << "@#{key} = #{xpath_escape(value)}"
  end

  if attribute_conditions.any?
    query << "[#{attribute_conditions.join(' and ')}]"
  end
end

#add_content_condition_to(query) ⇒ Object



59
60
61
62
63
# File 'lib/webrat/core/matchers/have_xpath.rb', line 59

def add_content_condition_to(query)
  if @options[:content]
    query << "[contains(., #{xpath_escape(@options[:content])})]"
  end
end

#add_options_conditions_to(query) ⇒ Object



41
42
43
44
# File 'lib/webrat/core/matchers/have_xpath.rb', line 41

def add_options_conditions_to(query)
  add_attributes_conditions_to(query)
  add_content_condition_to(query)
end

#failure_messageObject

Returns

String

The failure message.



71
72
73
# File 'lib/webrat/core/matchers/have_xpath.rb', line 71

def failure_message
  "expected following text to match xpath #{@expected}:\n#{@document}"
end

#matches(stringlike) ⇒ Object



24
25
26
# File 'lib/webrat/core/matchers/have_xpath.rb', line 24

def matches(stringlike)
  nokogiri_matches(stringlike)
end

#matches?(stringlike, &block) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
# File 'lib/webrat/core/matchers/have_xpath.rb', line 13

def matches?(stringlike, &block)
  @block ||= block
  matched = matches(stringlike)

  if @options[:count]
    matched.size == @options[:count].to_i && (!@block || @block.call(matched))
  else
    matched.any? && (!@block || @block.call(matched))
  end
end

#negative_failure_messageObject

Returns

String

The failure message to be displayed in negative matches.



77
78
79
# File 'lib/webrat/core/matchers/have_xpath.rb', line 77

def negative_failure_message
  "expected following text to not match xpath #{@expected}:\n#{@document}"
end

#nokogiri_matches(stringlike) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/webrat/core/matchers/have_xpath.rb', line 28

def nokogiri_matches(stringlike)
  if Nokogiri::XML::NodeSet === stringlike
    @query = query.gsub(%r'^//', './/')
  else
    @query = query
  end

  add_options_conditions_to(@query)

  @document = Webrat::XML.document(stringlike)
  @document.xpath(*@query)
end

#queryObject



65
66
67
# File 'lib/webrat/core/matchers/have_xpath.rb', line 65

def query
  @expected
end