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

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

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

- (Object) add_attributes_conditions_to(query)



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

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

- (Object) add_content_condition_to(query)



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

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

- (Object) add_options_conditions_to(query)



43
44
45
46
# File 'lib/webrat/core/matchers/have_xpath.rb', line 43

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

- (Object) failure_message

Returns

String

The failure message.



73
74
75
# File 'lib/webrat/core/matchers/have_xpath.rb', line 73

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

- (Object) matches(stringlike)



26
27
28
# File 'lib/webrat/core/matchers/have_xpath.rb', line 26

def matches(stringlike)
  nokogiri_matches(stringlike)
end

- (Boolean) matches?(stringlike, &block)

Returns:

  • (Boolean)


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

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

  @block.call(matched) if @block

  if @options[:count]
    matched.size == @options[:count].to_i
  else
    matched.any?
  end
end

- (Object) negative_failure_message

Returns

String

The failure message to be displayed in negative matches.



79
80
81
# File 'lib/webrat/core/matchers/have_xpath.rb', line 79

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

- (Object) nokogiri_matches(stringlike)



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

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

- (Object) query



67
68
69
# File 'lib/webrat/core/matchers/have_xpath.rb', line 67

def query
  @expected
end