Method: RSpecHtmlMatchers#have_tag

Defined in:
lib/rspec-html-matchers.rb

#have_tag(tag, options = {}) { ... } ⇒ Object

tag assertion, this is the core of functionality, other matchers are shortcuts to this matcher

Examples:

expect(rendered).to have_tag('div')
expect(rendered).to have_tag('h1.header')
expect(rendered).to have_tag('div#footer')
expect(rendered).to have_tag('input#email', :with => { :name => 'user[email]', :type => 'email' } )
expect(rendered).to have_tag('div', :count => 3)            # matches exactly 3 'div' tags
expect(rendered).to have_tag('div', :count => 3..7)         # shortcut for have_tag('div', :minimum => 3, :maximum => 7)
expect(rendered).to have_tag('div', :minimum => 3)          # matches more(or equal) than 3 'div' tags
expect(rendered).to have_tag('div', :maximum => 3)          # matches less(or equal) than 3 'div' tags
expect(rendered).to have_tag('p', :text => 'some content')  # will match "<p>some content</p>"
expect(rendered).to have_tag('p', :text => /some content/i) # will match "<p>sOme cOntEnt</p>"
expect(rendered).to have_tag('textarea', :with => {:name => 'user[description]'}, :text => "I like pie")
expect("<html>
  <body>
    <h1>some html document</h1>
  </body>
 </html>").to have_tag('body') { with_tag('h1', :text => 'some html document') }
expect('<div class="one two">').to have_tag('div', :with => { :class => ['two', 'one'] })
expect('<div class="one two">').to have_tag('div', :with => { :class => 'two one' })

Parameters:

  • tag (String)

    css selector for tag you want to match, e.g. ‘div’, ‘section#my’, ‘article.red’

  • options (Hash) (defaults to: {})

    options hash(see below)

Options Hash (options):

  • :with (Hash)

    hash with html attributes, within this, :class option have special meaning, you may specify it as array of expected classes or string of classes separated by spaces, order does not matter

  • :count (Fixnum)

    for tag count matching(ATTENTION: do not use :count with :minimum(:min) or :maximum(:max))

  • :count (Range)

    not strict tag count matching, count of tags should be in specified range

  • :minimum (Fixnum)

    minimum count of elements to match

  • :min (Fixnum)

    same as :minimum

  • :maximum (Fixnum)

    maximum count of elements to match

  • :max (Fixnum)

    same as :maximum

  • :text (String/Regexp)

    to match tag content, could be either String or Regexp

Yields:

  • block where you should put with_tag, without_tag and/or other matchers



62
63
64
65
66
# File 'lib/rspec-html-matchers.rb', line 62

def have_tag tag, options = {}, &block
  # for backwards compatibility with rpecs have tag:
  options = { :text => options } if options.is_a?(String) || options.is_a?(Regexp)
  @__current_scope_for_nokogiri_matcher = HaveTag.new(tag, options, &block)
end