Class: Brut::SpecSupport::EnhancedNode
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Brut::SpecSupport::EnhancedNode
- Includes:
- RSpec::Matchers
- Defined in:
- lib/brut/spec_support/enhanced_node.rb
Overview
A delegator to a Nokogiri Node that provides convienience methods for navigating the DOM inside a test.
Instance Method Summary collapse
-
#e(css_selector) ⇒ Object
Return the only Nokogiri::XML::Node for the given CSS selector, if it exists.
-
#e!(css_selector) ⇒ Object
Assert exactly one Nokogiri::XML::Node exists for the given CSS selector and return it.
-
#first!(css_selector) ⇒ Object
Return ths first Nokogiri::XML::Node for the given CSS selector.
Instance Method Details
#e(css_selector) ⇒ Object
Return the only Nokogiri::XML::Node for the given CSS selector, if it exists. If the selector matches more than one element, the test fails. If the selector matches one element, it is returned, and nil is returned if no elements match.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/brut/spec_support/enhanced_node.rb', line 11 def e(css_selector) element = css(css_selector) if (element.kind_of?(Nokogiri::XML::NodeSet)) expect(element.length).to be < 2 first_element = element.first if first_element return Brut::SpecSupport::EnhancedNode.new(first_element) else return nil end else expect([Nokogiri::XML::Node, Nokogiri::XML::Element]).to include(element.class) return Brut::SpecSupport::EnhancedNode.new(element) end end |
#e!(css_selector) ⇒ Object
Assert exactly one Nokogiri::XML::Node exists for the given CSS selector and return it. If there is not exactly one matching node, the test fails.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/brut/spec_support/enhanced_node.rb', line 29 def e!(css_selector) element = css(css_selector) if (element.kind_of?(Nokogiri::XML::NodeSet)) expect(element.length).to eq(1),"#{css_selector} matched #{element.length} elements, not exactly 1:\n\n#{to_html}" return Brut::SpecSupport::EnhancedNode.new(element.first) else expect([Nokogiri::XML::Node, Nokogiri::XML::Element]).to include(element.class) return Brut::SpecSupport::EnhancedNode.new(element) end end |
#first!(css_selector) ⇒ Object
Return ths first Nokogiri::XML::Node for the given CSS selector. If there are no matching nodes, the test fails.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/brut/spec_support/enhanced_node.rb', line 42 def first!(css_selector) element = css(css_selector) if (element.kind_of?(Nokogiri::XML::NodeSet)) expect(element.first).not_to eq(nil), "No elements matching #{css_selector}" return Brut::SpecSupport::EnhancedNode.new(element.first) else expect([Nokogiri::XML::Node, Nokogiri::XML::Element]).to include(element.class) return Brut::SpecSupport::EnhancedNode.new(element) end end |