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, &block) ⇒ HaveXpath

Returns a new instance of HaveXpath.



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

def initialize(expected, &block)
  @expected = expected
  @block    = block
end

Instance Method Details

#failure_messageObject

Returns

String

The failure message.



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

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

#matches?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches?(stringlike)
  if Webrat.configuration.parse_with_nokogiri?
    matches_nokogiri?(stringlike)
  else
    matches_rexml?(stringlike)
  end
end

#matches_nokogiri?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/webrat/core/matchers/have_xpath.rb', line 41

def matches_nokogiri?(stringlike)
  if Nokogiri::XML::NodeSet === stringlike
    @query = query.map { |q| q.gsub(%r'//', './') }
  else
    @query = query
  end
  
  @document = Webrat::XML.document(stringlike)
  matched = @document.xpath(*@query)
  matched.any? && (!@block || @block.call(matched))
end

#matches_rexml?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/webrat/core/matchers/have_xpath.rb', line 21

def matches_rexml?(stringlike)
  if REXML::Node === stringlike || Array === stringlike
    @query = query.map { |q| q.gsub(%r'//', './') }
  else
    @query = query
  end

  @document = Webrat.rexml_document(stringlike)

  matched = @query.map do |q|
    if @document.is_a?(Array)
      @document.map { |d| REXML::XPath.match(d, q) }
    else
      REXML::XPath.match(@document, q)
    end
  end.flatten.compact

  matched.any? && (!@block || @block.call(matched))
end

#negative_failure_messageObject

Returns

String

The failure message to be displayed in negative matches.



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

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

#queryObject



53
54
55
# File 'lib/webrat/core/matchers/have_xpath.rb', line 53

def query
  [@expected].flatten.compact
end