rails-dom-testing-1.0.6/0000755000175000017500000000000012563156012015273 5ustar terceiroterceirorails-dom-testing-1.0.6/lib/0000755000175000017500000000000012563156012016041 5ustar terceiroterceirorails-dom-testing-1.0.6/lib/rails/0000755000175000017500000000000012563156012017153 5ustar terceiroterceirorails-dom-testing-1.0.6/lib/rails/dom/0000755000175000017500000000000012563156012017732 5ustar terceiroterceirorails-dom-testing-1.0.6/lib/rails/dom/testing/0000755000175000017500000000000012563156012021407 5ustar terceiroterceirorails-dom-testing-1.0.6/lib/rails/dom/testing/assertions/0000755000175000017500000000000012563156012023601 5ustar terceiroterceirorails-dom-testing-1.0.6/lib/rails/dom/testing/assertions/tag_assertions.rb0000644000175000017500000001712212563156012027156 0ustar terceiroterceirorequire 'active_support/deprecation' require 'rails/deprecated_sanitizer/html-scanner' module Rails module Dom module Testing module Assertions # Pair of assertions to testing elements in the HTML output of the response. module TagAssertions # Asserts that there is a tag/node/element in the body of the response # that meets all of the given conditions. The +conditions+ parameter must # be a hash of any of the following keys (all are optional): # # * :tag: the node type must match the corresponding value # * :attributes: a hash. The node's attributes must match the # corresponding values in the hash. # * :parent: a hash. The node's parent must match the # corresponding hash. # * :child: a hash. At least one of the node's immediate children # must meet the criteria described by the hash. # * :ancestor: a hash. At least one of the node's ancestors must # meet the criteria described by the hash. # * :descendant: a hash. At least one of the node's descendants # must meet the criteria described by the hash. # * :sibling: a hash. At least one of the node's siblings must # meet the criteria described by the hash. # * :after: a hash. The node must be after any sibling meeting # the criteria described by the hash, and at least one sibling must match. # * :before: a hash. The node must be before any sibling meeting # the criteria described by the hash, and at least one sibling must match. # * :children: a hash, for counting children of a node. Accepts # the keys: # * :count: either a number or a range which must equal (or # include) the number of children that match. # * :less_than: the number of matching children must be less # than this number. # * :greater_than: the number of matching children must be # greater than this number. # * :only: another hash consisting of the keys to use # to match on the children, and only matching children will be # counted. # * :content: the textual content of the node must match the # given value. This will not match HTML tags in the body of a # tag--only text. # # Conditions are matched using the following algorithm: # # * if the condition is a string, it must be a substring of the value. # * if the condition is a regexp, it must match the value. # * if the condition is a number, the value must match number.to_s. # * if the condition is +true+, the value must not be +nil+. # * if the condition is +false+ or +nil+, the value must be +nil+. # # # Assert that there is a "span" tag # assert_tag tag: "span" # # # Assert that there is a "span" tag with id="x" # assert_tag tag: "span", attributes: { id: "x" } # # # Assert that there is a "span" tag using the short-hand # assert_tag :span # # # Assert that there is a "span" tag with id="x" using the short-hand # assert_tag :span, attributes: { id: "x" } # # # Assert that there is a "span" inside of a "div" # assert_tag tag: "span", parent: { tag: "div" } # # # Assert that there is a "span" somewhere inside a table # assert_tag tag: "span", ancestor: { tag: "table" } # # # Assert that there is a "span" with at least one "em" child # assert_tag tag: "span", child: { tag: "em" } # # # Assert that there is a "span" containing a (possibly nested) # # "strong" tag. # assert_tag tag: "span", descendant: { tag: "strong" } # # # Assert that there is a "span" containing between 2 and 4 "em" tags # # as immediate children # assert_tag tag: "span", # children: { count: 2..4, only: { tag: "em" } } # # # Get funky: assert that there is a "div", with an "ul" ancestor # # and an "li" parent (with "class" = "enum"), and containing a # # "span" descendant that contains text matching /hello world/ # assert_tag tag: "div", # ancestor: { tag: "ul" }, # parent: { tag: "li", # attributes: { class: "enum" } }, # descendant: { tag: "span", # child: /hello world/ } # # Please note: +assert_tag+ and +assert_no_tag+ only work # with well-formed XHTML. They recognize a few tags as implicitly self-closing # (like br and hr and such) but will not work correctly with tags # that allow optional closing tags (p, li, td). You must explicitly # close all of your tags to use these assertions. def assert_tag(*opts) ActiveSupport::Deprecation.warn("assert_tag is deprecated and will be removed at Rails 5. Use assert_select to get the same feature.") opts = opts.size > 1 ? opts.last.merge({ tag: opts.first.to_s }) : opts.first tag = _find_tag(opts) assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}" end # Identical to +assert_tag+, but asserts that a matching tag does _not_ # exist. (See +assert_tag+ for a full discussion of the syntax.) # # # Assert that there is not a "div" containing a "p" # assert_no_tag tag: "div", descendant: { tag: "p" } # # # Assert that an unordered list is empty # assert_no_tag tag: "ul", descendant: { tag: "li" } # # # Assert that there is not a "p" tag with between 1 to 3 "img" tags # # as immediate children # assert_no_tag tag: "p", # children: { count: 1..3, only: { tag: "img" } } def assert_no_tag(*opts) ActiveSupport::Deprecation.warn("assert_no_tag is deprecated and will be removed at Rails 5. Use assert_select to get the same feature.") opts = opts.size > 1 ? opts.last.merge({ tag: opts.first.to_s }) : opts.first tag = _find_tag(opts) assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}" end def find_tag(conditions) ActiveSupport::Deprecation.warn("find_tag is deprecated and will be removed at Rails 5 without replacement.") _find_tag(conditions) end def find_all_tag(conditions) ActiveSupport::Deprecation.warn("find_all_tag is deprecated and will be removed at Rails 5 without replacement. Use assert_select to get the same feature.") html_scanner_document.find_all(conditions) end private def _find_tag(conditions) html_scanner_document.find(conditions) end def html_scanner_document xml = @response.content_type =~ /xml$/ @html_document ||= HTML::Document.new(@response.body, false, xml) end end end end end end rails-dom-testing-1.0.6/lib/rails/dom/testing/assertions/selector_assertions/0000755000175000017500000000000012563156012027673 5ustar terceiroterceirorails-dom-testing-1.0.6/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb0000644000175000017500000000616212563156012033071 0ustar terceiroterceirorequire 'active_support/core_ext/module/attribute_accessors' require_relative 'substitution_context' class HTMLSelector #:nodoc: attr_reader :selector, :tests, :message def initialize(values, previous_selection = nil, &root_fallback) @values = values @root = extract_root(previous_selection, root_fallback) @selector = extract_selector @tests = extract_equality_tests @message = @values.shift if @values.shift raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type" end end def select filter @root.css(selector, context) end private NO_STRIP = %w{pre script style textarea} mattr_reader(:context) { SubstitutionContext.new } def filter(matches) match_with = tests[:text] || tests[:html] return matches if matches.empty? || !match_with content_mismatch = nil text_matches = tests.has_key?(:text) regex_matching = match_with.is_a?(Regexp) remaining = matches.reject do |match| # Preserve markup with to_s for html elements content = text_matches ? match.text : match.children.to_s content.strip! unless NO_STRIP.include?(match.name) content.sub!(/\A\n/, '') if text_matches && match.name == "textarea" next if regex_matching ? (content =~ match_with) : (content == match_with) content_mismatch ||= sprintf("<%s> expected but was\n<%s>.", match_with, content) true end @message ||= content_mismatch if remaining.empty? Nokogiri::XML::NodeSet.new(matches.document, remaining) end def extract_root(previous_selection, root_fallback) possible_root = @values.first if possible_root == nil raise ArgumentError, 'First argument is either selector or element ' \ 'to select, but nil found. Perhaps you called assert_select with ' \ 'an element that does not exist?' elsif possible_root.respond_to?(:css) @values.shift # remove the root, so selector is the first argument possible_root elsif previous_selection previous_selection else root_fallback.call end end def extract_selector selector = @values.shift unless selector.is_a? String raise ArgumentError, "Expecting a selector as the first argument" end context.substitute!(selector, @values) selector end def extract_equality_tests comparisons = {} case comparator = @values.shift when Hash comparisons = comparator when String, Regexp comparisons[:text] = comparator when Integer comparisons[:count] = comparator when Range comparisons[:minimum] = comparator.begin comparisons[:maximum] = comparator.end when FalseClass comparisons[:count] = 0 when NilClass, TrueClass comparisons[:minimum] = 1 else raise ArgumentError, "I don't understand what you're trying to match" end # By default we're looking for at least one match. if comparisons[:count] comparisons[:minimum] = comparisons[:maximum] = comparisons[:count] else comparisons[:minimum] ||= 1 end comparisons end end rails-dom-testing-1.0.6/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb0000644000175000017500000000115712563156012034544 0ustar terceiroterceiroclass SubstitutionContext def initialize @substitute = '?' end def substitute!(selector, values) while !values.empty? && substitutable?(values.first) && selector.index(@substitute) selector.sub! @substitute, matcher_for(values.shift) end end def match(matches, attribute, matcher) matches.find_all { |node| node[attribute] =~ Regexp.new(matcher) } end private def matcher_for(value) value.to_s.inspect # Nokogiri doesn't like arbitrary values without quotes, hence inspect. end def substitutable?(value) value.is_a?(String) || value.is_a?(Regexp) end end rails-dom-testing-1.0.6/lib/rails/dom/testing/assertions/selector_assertions/count_describable.rb0000644000175000017500000000113712563156012033671 0ustar terceiroterceirorequire 'active_support/concern' module CountDescripable extend ActiveSupport::Concern private def count_description(min, max, count) #:nodoc: if min && max && (max != min) "between #{min} and #{max} elements" elsif min && max && max == min && count "exactly #{count} #{pluralize_element(min)}" elsif min && !(min == 1 && max == 1) "at least #{min} #{pluralize_element(min)}" elsif max "at most #{max} #{pluralize_element(max)}" end end def pluralize_element(quantity) quantity == 1 ? 'element' : 'elements' end end rails-dom-testing-1.0.6/lib/rails/dom/testing/assertions/dom_assertions.rb0000644000175000017500000000537312563156012027167 0ustar terceiroterceiromodule Rails module Dom module Testing module Assertions module DomAssertions # \Test two HTML strings for equivalency (e.g., equal even when attributes are in another order) # # # assert that the referenced method generates the appropriate HTML string # assert_dom_equal 'Apples', link_to("Apples", "http://www.example.com") def assert_dom_equal(expected, actual, message = nil) expected_dom, actual_dom = fragment(expected), fragment(actual) message ||= "Expected: #{expected}\nActual: #{actual}" assert compare_doms(expected_dom, actual_dom), message end # The negated form of +assert_dom_equal+. # # # assert that the referenced method does not generate the specified HTML string # assert_dom_not_equal 'Apples', link_to("Oranges", "http://www.example.com") def assert_dom_not_equal(expected, actual, message = nil) expected_dom, actual_dom = fragment(expected), fragment(actual) message ||= "Expected: #{expected}\nActual: #{actual}" assert_not compare_doms(expected_dom, actual_dom), message end protected def compare_doms(expected, actual) return false unless expected.children.size == actual.children.size expected.children.each_with_index do |child, i| return false unless equal_children?(child, actual.children[i]) end true end def equal_children?(child, other_child) return false unless child.type == other_child.type if child.element? child.name == other_child.name && equal_attribute_nodes?(child.attribute_nodes, other_child.attribute_nodes) && compare_doms(child, other_child) else child.to_s == other_child.to_s end end def equal_attribute_nodes?(nodes, other_nodes) return false unless nodes.size == other_nodes.size nodes = nodes.sort_by(&:name) other_nodes = other_nodes.sort_by(&:name) nodes.each_with_index do |attr, i| return false unless equal_attribute?(attr, other_nodes[i]) end true end def equal_attribute?(attr, other_attr) attr.name == other_attr.name && attr.value == other_attr.value end private def fragment(text) Nokogiri::HTML::DocumentFragment.parse(text) end end end end end end rails-dom-testing-1.0.6/lib/rails/dom/testing/assertions/selector_assertions.rb0000644000175000017500000003141112563156012030220 0ustar terceiroterceirorequire 'active_support/deprecation' require_relative 'selector_assertions/count_describable' require_relative 'selector_assertions/html_selector' module Rails module Dom module Testing module Assertions # Adds the +assert_select+ method for use in Rails functional # test cases, which can be used to make assertions on the response HTML of a controller # action. You can also call +assert_select+ within another +assert_select+ to # make assertions on elements selected by the enclosing assertion. # # Use +css_select+ to select elements without making an assertions, either # from the response HTML or elements selected by the enclosing assertion. # # In addition to HTML responses, you can make the following assertions: # # * +assert_select_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions. # * +assert_select_email+ - Assertions on the HTML body of an e-mail. module SelectorAssertions # Select and return all matching elements. # # If called with a single argument, uses that argument as a selector. # Called without an element +css_select+ selects from # the element returned in +document_root_element+ # # The default implementation of +document_root_element+ raises an exception explaining this. # # Returns an empty Nokogiri::XML::NodeSet if no match is found. # # If called with two arguments, uses the first argument as the root # element and the second argument as the selector. Attempts to match the # root element and any of its children. # Returns an empty Nokogiri::XML::NodeSet if no match is found. # # The selector may be a CSS selector expression (String). # css_select returns nil if called with an invalid css selector. # # # Selects all div tags # divs = css_select("div") # # # Selects all paragraph tags and does something interesting # pars = css_select("p") # pars.each do |par| # # Do something fun with paragraphs here... # end # # # Selects all list items in unordered lists # items = css_select("ul>li") # # # Selects all form tags and then all inputs inside the form # forms = css_select("form") # forms.each do |form| # inputs = css_select(form, "input") # ... # end def css_select(*args) raise ArgumentError, "you at least need a selector argument" if args.empty? root = args.size == 1 ? document_root_element : args.shift nodeset(root).css(args.first) rescue Nokogiri::CSS::SyntaxError => e ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2)) return end # An assertion that selects elements and makes one or more equality tests. # # If the first argument is an element, selects all matching elements # starting from (and including) that element and all its children in # depth-first order. # # If no element is specified +assert_select+ selects from # the element returned in +document_root_element+ # unless +assert_select+ is called from within an +assert_select+ block. # Override +document_root_element+ to tell +assert_select+ what to select from. # The default implementation raises an exception explaining this. # # When called with a block +assert_select+ passes an array of selected elements # to the block. Calling +assert_select+ from the block, with no element specified, # runs the assertion on the complete set of elements selected by the enclosing assertion. # Alternatively the array may be iterated through so that +assert_select+ can be called # separately for each element. # # # ==== Example # If the response contains two ordered lists, each with four list elements then: # assert_select "ol" do |elements| # elements.each do |element| # assert_select element, "li", 4 # end # end # # will pass, as will: # assert_select "ol" do # assert_select "li", 8 # end # # The selector may be a CSS selector expression (String) or an expression # with substitution values (Array). # Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution. # assert_select returns nil if called with an invalid css selector. # # assert_select "div:match('id', ?)", /\d+/ # # === Equality Tests # # The equality test may be one of the following: # * true - Assertion is true if at least one element selected. # * false - Assertion is true if no element selected. # * String/Regexp - Assertion is true if the text value of at least # one element matches the string or regular expression. # * Integer - Assertion is true if exactly that number of # elements are selected. # * Range - Assertion is true if the number of selected # elements fit the range. # If no equality test specified, the assertion is true if at least one # element selected. # # To perform more than one equality tests, use a hash with the following keys: # * :text - Narrow the selection to elements that have this text # value (string or regexp). # * :html - Narrow the selection to elements that have this HTML # content (string or regexp). # * :count - Assertion is true if the number of selected elements # is equal to this value. # * :minimum - Assertion is true if the number of selected # elements is at least this value. # * :maximum - Assertion is true if the number of selected # elements is at most this value. # # If the method is called with a block, once all equality tests are # evaluated the block is called with an array of all matched elements. # # # At least one form element # assert_select "form" # # # Form element includes four input fields # assert_select "form input", 4 # # # Page title is "Welcome" # assert_select "title", "Welcome" # # # Page title is "Welcome" and there is only one title element # assert_select "title", {count: 1, text: "Welcome"}, # "Wrong title or more than one title element" # # # Page contains no forms # assert_select "form", false, "This page must contain no forms" # # # Test the content and style # assert_select "body div.header ul.menu" # # # Use substitution values # assert_select "ol>li:match('id', ?)", /item-\d+/ # # # All input fields in the form have a name # assert_select "form input" do # assert_select ":match('name', ?)", /.+/ # Not empty # end def assert_select(*args, &block) @selected ||= nil selector = HTMLSelector.new(args, @selected) { nodeset document_root_element } if selecting_no_body?(selector) assert true return end selector.select.tap do |matches| assert_size_match!(matches.size, selector.tests, selector.selector, selector.message) nest_selection(matches, &block) if block_given? && !matches.empty? end rescue Nokogiri::CSS::SyntaxError => e ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2)) return end # Extracts the content of an element, treats it as encoded HTML and runs # nested assertion on it. # # You typically call this method within another assertion to operate on # all currently selected elements. You can also pass an element or array # of elements. # # The content of each element is un-encoded, and wrapped in the root # element +encoded+. It then calls the block with all un-encoded elements. # # # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix) # assert_select "feed[xmlns='http://www.w3.org/2005/Atom']" do # # Select each entry item and then the title item # assert_select "entry>title" do # # Run assertions on the encoded title elements # assert_select_encoded do # assert_select "b" # end # end # end # # # # Selects all paragraph tags from within the description of an RSS feed # assert_select "rss[version=2.0]" do # # Select description element of each feed item. # assert_select "channel>item>description" do # # Run assertions on the encoded elements. # assert_select_encoded do # assert_select "p" # end # end # end def assert_select_encoded(element = nil, &block) if !element && !@selected raise ArgumentError, "Element is required when called from a nonnested assert_select" end content = nodeset(element || @selected).map do |elem| elem.children.select(&:cdata?).map(&:content) end.join selected = Nokogiri::HTML::DocumentFragment.parse(content) nest_selection(selected) do if content.empty? yield selected else assert_select ":root", &block end end end # Extracts the body of an email and runs nested assertions on it. # # You must enable deliveries for this assertion to work, use: # ActionMailer::Base.perform_deliveries = true # # assert_select_email do # assert_select "h1", "Email alert" # end # # assert_select_email do # items = assert_select "ol>li" # items.each do # # Work with items here... # end # end def assert_select_email(&block) deliveries = ActionMailer::Base.deliveries assert !deliveries.empty?, "No e-mail in delivery list" deliveries.each do |delivery| (delivery.parts.empty? ? [delivery] : delivery.parts).each do |part| if part["Content-Type"].to_s =~ /^text\/html\W/ root = Nokogiri::HTML::DocumentFragment.parse(part.body.to_s) assert_select root, ":root", &block end end end end private include CountDescripable def document_root_element raise NotImplementedError, 'Implementing document_root_element makes ' \ 'assert_select work without needing to specify an element to select from.' end # +equals+ must contain :minimum, :maximum and :count keys def assert_size_match!(size, equals, css_selector, message = nil) min, max, count = equals[:minimum], equals[:maximum], equals[:count] message ||= %(Expected #{count_description(min, max, count)} matching "#{css_selector}", found #{size}.) if count assert_equal count, size, message else assert_operator size, :>=, min, message if min assert_operator size, :<=, max, message if max end end def selecting_no_body?(html_selector) # Nokogiri gives the document a body element. Which means we can't # run an assertion expecting there to not be a body. html_selector.selector == 'body' && html_selector.tests[:count] == 0 end def nest_selection(selection) # Set @selected to allow nested assert_select. # Can be nested several levels deep. old_selected, @selected = @selected, selection yield @selected ensure @selected = old_selected end def nodeset(node) if node.is_a?(Nokogiri::XML::NodeSet) node else Nokogiri::XML::NodeSet.new(node.document, [node]) end end end end end end end rails-dom-testing-1.0.6/lib/rails/dom/testing/assertions.rb0000644000175000017500000000102312563156012024122 0ustar terceiroterceirorequire 'active_support/concern' require 'nokogiri' module Rails module Dom module Testing module Assertions autoload :DomAssertions, 'rails/dom/testing/assertions/dom_assertions' autoload :SelectorAssertions, 'rails/dom/testing/assertions/selector_assertions' autoload :TagAssertions, 'rails/dom/testing/assertions/tag_assertions' extend ActiveSupport::Concern include DomAssertions include SelectorAssertions include TagAssertions end end end endrails-dom-testing-1.0.6/lib/rails/dom/testing/version.rb0000644000175000017500000000012712563156012023421 0ustar terceiroterceiromodule Rails module Dom module Testing VERSION = "1.0.6" end end end rails-dom-testing-1.0.6/lib/rails-dom-testing.rb0000644000175000017500000000004612563156012021730 0ustar terceiroterceirorequire 'rails/dom/testing/assertions'rails-dom-testing-1.0.6/LICENSE.txt0000644000175000017500000000206312563156012017117 0ustar terceiroterceiroCopyright (c) 2013 Kasper Timm Hansen MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rails-dom-testing-1.0.6/README.md0000644000175000017500000000510212563156012016550 0ustar terceiroterceiro# Rails::Dom::Testing [![Build Status](https://travis-ci.org/rails/rails-dom-testing.svg)](https://travis-ci.org/rails/rails-dom-testing) This gem is responsible for comparing HTML doms and asserting that DOM elements are present in Rails applications. Doms are compared via `assert_dom_equal` and `assert_dom_not_equal`. Elements are asserted via `assert_select`, `assert_select_encoded`, `assert_select_email` and a subset of the dom can be selected with `css_select`. The gem is developed for Rails 4.2 and above, and will not work on previous versions. ## Deprecation warnings when upgrading to Rails 4.2: Nokogiri is slightly more strict about the format of css selectors than the previous implementation. That's why you have warnings like: ``` DEPRECATION WARNING: The assertion was not run because of an invalid css selector. ``` Check the 4.2 release notes [section on `assert_select`](http://edgeguides.rubyonrails.org/4_2_release_notes.html#assert-select) for help. ## Installation Add this line to your application's Gemfile: gem 'rails-dom-testing' And then execute: $ bundle Or install it yourself as: $ gem install rails-dom-testing ## Usage ### Dom Assertions ```ruby assert_dom_equal '

Lingua França

', '

Lingua França

' assert_dom_not_equal '

Portuguese

', '

Danish

' ``` ### Selector Assertions ```ruby # implicitly selects from the document_root_element css_select '.hello' # => Nokogiri::XML::NodeSet of elements with hello class # select from a supplied node. assert_select asserts elements exist. assert_select document_root_element.at('.hello'), '.goodbye' # elements in CDATA encoded sections can also be selected assert_select_encoded '#out-of-your-element' # assert elements within an html email exists assert_select_email '#you-got-mail' ``` The documentation in [selector_assertions.rb](https://github.com/kaspth/rails-dom-testing/blob/master/lib/rails/dom/testing/assertions/selector_assertions.rb) goes into a lot more detail of how selector assertions can be used. ## Read more Under the hood the doms are parsed with Nokogiri and you'll generally be working with these two classes: - [`Nokogiri::XML::Node`](http://nokogiri.org/Nokogiri/XML/Node.html) - [`Nokogiri::XML::NodeSet`](http://nokogiri.org/Nokogiri/XML/NodeSet.html) Read more about Nokogiri: - [Nokogiri](http://nokogiri.org) ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request rails-dom-testing-1.0.6/test/0000755000175000017500000000000012563156012016252 5ustar terceiroterceirorails-dom-testing-1.0.6/test/tag_assertions_test.rb0000644000175000017500000001464512563156012022675 0ustar terceiroterceirorequire 'test_helper' require 'rails/dom/testing/assertions/tag_assertions' HTML_TEST_OUTPUT = <
Name:
HTML class AssertTagTest < ActiveSupport::TestCase include Rails::Dom::Testing::Assertions::TagAssertions class FakeResponse attr_accessor :content_type, :body def initialize(content_type, body) @content_type, @body = content_type, body end end setup do @response = FakeResponse.new 'html', HTML_TEST_OUTPUT end def test_assert_tag_tag assert_deprecated do # there is a 'form' tag assert_tag tag: 'form' # there is not an 'hr' tag assert_no_tag tag: 'hr' end end def test_assert_tag_attributes assert_deprecated do # there is a tag with an 'id' of 'bar' assert_tag attributes: { id: "bar" } # there is no tag with a 'name' of 'baz' assert_no_tag attributes: { name: "baz" } end end def test_assert_tag_parent assert_deprecated do # there is a tag with a parent 'form' tag assert_tag parent: { tag: "form" } # there is no tag with a parent of 'input' assert_no_tag parent: { tag: "input" } end end def test_assert_tag_child assert_deprecated do # there is a tag with a child 'input' tag assert_tag child: { tag: "input" } # there is no tag with a child 'strong' tag assert_no_tag child: { tag: "strong" } end end def test_assert_tag_ancestor assert_deprecated do # there is a 'li' tag with an ancestor having an id of 'foo' assert_tag ancestor: { attributes: { id: "foo" } }, tag: "li" # there is no tag of any kind with an ancestor having an href matching 'foo' assert_no_tag ancestor: { attributes: { href: /foo/ } } end end def test_assert_tag_descendant assert_deprecated do # there is a tag with a descendant 'li' tag assert_tag descendant: { tag: "li" } # there is no tag with a descendant 'html' tag assert_no_tag descendant: { tag: "html" } end end def test_assert_tag_sibling assert_deprecated do # there is a tag with a sibling of class 'item' assert_tag sibling: { attributes: { class: "item" } } # there is no tag with a sibling 'ul' tag assert_no_tag sibling: { tag: "ul" } end end def test_assert_tag_after assert_deprecated do # there is a tag following a sibling 'div' tag assert_tag after: { tag: "div" } # there is no tag following a sibling tag with id 'bar' assert_no_tag after: { attributes: { id: "bar" } } end end def test_assert_tag_before assert_deprecated do # there is a tag preceding a tag with id 'bar' assert_tag before: { attributes: { id: "bar" } } # there is no tag preceding a 'form' tag assert_no_tag before: { tag: "form" } end end def test_assert_tag_children_count assert_deprecated do # there is a tag with 2 children assert_tag children: { count: 2 } # in particular, there is a