Sha256: b8fe5b7d33e41a0f3170c642f72dc91241b4d85cc8467e5f835925b7a4eda304

Contents?: true

Size: 1.99 KB

Versions: 20

Compression:

Stored size: 1.99 KB

Contents

require "webrat/core/matchers/have_xpath"

module Webrat
  module Matchers
    
    class HaveSelector < HaveXpath #:nodoc:
      # ==== Returns
      # String:: The failure message.
      def failure_message
        "expected following output to contain a #{tag_inspect} tag:\n#{@document}"
      end

      # ==== Returns
      # String:: The failure message to be displayed in negative matches.
      def negative_failure_message
        "expected following output to omit a #{tag_inspect}:\n#{@document}"
      end
      
      def tag_inspect
        options = @options.dup
        count = options.delete(:count)
        content = options.delete(:content)

        html = "<#{@expected}"
        options.each do |k,v|
          html << " #{k}='#{v}'"
        end

        if content
          html << ">#{content}</#{@expected}>"
        else
          html << "/>"
        end

        html
      end

      def query
        Nokogiri::CSS.parse(@expected.to_s).map do |ast|
          ast.to_xpath
        end.first
      end
      
    end
    
    # Matches HTML content against a CSS 3 selector.
    #
    # ==== Parameters
    # expected<String>:: The CSS selector to look for.
    #
    # ==== Returns
    # HaveSelector:: A new have selector matcher.
    def have_selector(name, attributes = {}, &block)
      HaveSelector.new(name, attributes, &block)
    end
    alias_method :match_selector, :have_selector
    
    
    # Asserts that the body of the response contains
    # the supplied selector
    def assert_have_selector(name, attributes = {}, &block)
      matcher = HaveSelector.new(name, attributes, &block)
      assert matcher.matches?(response_body), matcher.failure_message
    end
    
    # Asserts that the body of the response
    # does not contain the supplied string or regepx
    def assert_have_no_selector(name, attributes = {}, &block)
      matcher = HaveSelector.new(name, attributes, &block)
      assert !matcher.matches?(response_body), matcher.negative_failure_message
    end
    
  end
end

Version data entries

20 entries across 20 versions & 8 rubygems

Version Path
diabolo-webrat-0.4.2 lib/webrat/core/matchers/have_selector.rb
hectoregm-webrat-0.4.2 lib/webrat/core/matchers/have_selector.rb
jferris-webrat-0.4.3.0.1238640835 lib/webrat/core/matchers/have_selector.rb
jferris-webrat-0.4.3 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.2.1 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.2.2 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.2.3 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.2.4 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.2.5 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.2 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.3.2 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.3.3 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.3.4 lib/webrat/core/matchers/have_selector.rb
mattwynne-cucover-0.1.0 examples/self_test/rails/vendor/gems/webrat-0.4.4/lib/webrat/core/matchers/have_selector.rb
mattwynne-cucover-0.1.1 examples/self_test/rails/vendor/gems/webrat-0.4.4/lib/webrat/core/matchers/have_selector.rb
mmurphy-webrat-0.4.4 lib/webrat/core/matchers/have_selector.rb
integrity-0.1.9 vendor/webrat/lib/webrat/core/matchers/have_selector.rb
webrat-0.4.2 lib/webrat/core/matchers/have_selector.rb
webrat-0.4.4 lib/webrat/core/matchers/have_selector.rb
webrat-0.4.3 lib/webrat/core/matchers/have_selector.rb