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, nsmap = {}, &block) ⇒ HaveXpath

Returns a new instance of HaveXpath.



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

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

Instance Method Details

#failure_messageObject

Returns

String

The failure message.



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

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

#matches?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#matches_nokogiri?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches_nokogiri?(stringlike)
  if Nokogiri::XML::NodeSet === stringlike
    @query = query.map { |q| q.gsub(%r'//', './') }
  else
    @query = query
  end

  @query << @nsmap if @nsmap
  @document = Webrat::XML.document(stringlike)
  matched = @document.xpath(*@query)
  matched.any? && (!@block || @block.call(matched))
end

#matches_rexml?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


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

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.



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

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

#queryObject



56
57
58
# File 'lib/webrat/core/matchers/have_xpath.rb', line 56

def query
  [@expected].flatten.compact
end