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, nsmap = {}, &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, 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_message ⇒ Object
Returns
- String
-
The failure message.
62 63 64 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 62 def "expected following text to match xpath #{@expected}:\n#{@document}" end |
#matches?(stringlike) ⇒ 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
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
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_message ⇒ Object
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 "expected following text to not match xpath #{@expected}:\n#{@document}" end |
#query ⇒ Object
56 57 58 |
# File 'lib/webrat/core/matchers/have_xpath.rb', line 56 def query [@expected].flatten.compact end |