Sha256: 958ffebe3844d7227ff08b9f7a8b5d2023f13f1bfab7e981e2a697ab11f70312

Contents?: true

Size: 1.95 KB

Versions: 77

Compression:

Stored size: 1.95 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

77 entries across 77 versions & 31 rubygems

Version Path
hardbap-webrat-0.5.2 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.4.2 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.4.3 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.4.4 lib/webrat/core/matchers/have_selector.rb
jwilger-webrat-0.4.4.5 lib/webrat/core/matchers/have_selector.rb
ohammersmith-webrat-0.4.4.100 lib/webrat/core/matchers/have_selector.rb
ohammersmith-webrat-0.4.4.98 lib/webrat/core/matchers/have_selector.rb
raldred-webrat-0.4.4.2 lib/webrat/core/matchers/have_selector.rb
sr-webrat-0.4.4.1 lib/webrat/core/matchers/have_selector.rb
winton-sum-0.1.0 vendor/webrat/lib/webrat/core/matchers/have_selector.rb
winton-sum-0.1.1 vendor/webrat/lib/webrat/core/matchers/have_selector.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/webrat-0.7.3/lib/webrat/core/matchers/have_selector.rb
webrat-0.7.3 lib/webrat/core/matchers/have_selector.rb
jbd-webrat-0.7.2.rails3 lib/webrat/core/matchers/have_selector.rb
webrat-0.7.2 lib/webrat/core/matchers/have_selector.rb
webrat-0.7.2.beta.2 lib/webrat/core/matchers/have_selector.rb
mutle-webrat-0.7.2.beta.1 lib/webrat/core/matchers/have_selector.rb
thoughtbot-webrat-0.7.2.pre lib/webrat/core/matchers/have_selector.rb
webrat-0.7.2.beta.1 lib/webrat/core/matchers/have_selector.rb
honkster-webrat-0.7.1.2 lib/webrat/core/matchers/have_selector.rb