Class: Webrat::Matchers::HaveXpath
- Defined in:
- lib/webrat/core/matchers/have_xpath.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Method Summary collapse
-
#failure_message ⇒ Object
Returns String:: The failure message.
-
#initialize(expected, &block) ⇒ HaveXpath
constructor
A new instance of HaveXpath.
- #matches?(stringlike) ⇒ Boolean
- #matches_nokogiri?(stringlike) ⇒ Boolean
- #matches_rexml?(stringlike) ⇒ Boolean
-
#negative_failure_message ⇒ Object
Returns String:: The failure message to be displayed in negative matches.
- #query ⇒ Object
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_message ⇒ Object
Returns
- String
-
The failure message.
59 60 61 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 59 def "expected following text to match xpath #{@expected}:\n#{@document}" end |
#matches?(stringlike) ⇒ 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
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
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_message ⇒ Object
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 "expected following text to not match xpath #{@expected}:\n#{@document}" end |
#query ⇒ Object
53 54 55 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 53 def query [@expected].flatten.compact end |