Code Monkey home page Code Monkey logo

rspec-html-matchers's Introduction

rspec-html-matchers

RSpec 2 matchers for testing your html.

Gem Version Build Status

Goals

Install

Add to your Gemfile in the :test group:

gem 'rspec-html-matchers'

as this gem requires nokogiri, here instructions for installing it.

Usage

so perharps your code produces following output:

<h1>Simple Form</h1>
<form action="/users" method="post">
<p>
  <input type="email" name="user[email]" />
</p>
<p>
  <input type="submit" id="special_submit" />
</p>
</form>

so you test it with following:

rendered.should have_tag('form', :with => { :action => '/users', :method => 'post' }) do
  with_tag "input", :with => { :name => "user[email]", :type => 'email' }
  with_tag "input#special_submit", :count => 1
  without_tag "h1", :text => 'unneeded tag'
  without_tag "p",  :text => /content/i
end

Example about should be self-descriptive, but if not refer to have_tag documentation

Input could be any html string. Let's take a look at these examples:

  • matching tags by css:

    # simple examples:
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p')
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag(:p)
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p#qwerty')
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p.qwe.rty')
    # more complicated examples:
    '<p class="qwe rty" id="qwerty"><strong>Para</strong>graph</p>'.should have_tag('p strong')
    '<p class="qwe rty" id="qwerty"><strong>Para</strong>graph</p>'.should have_tag('p#qwerty strong')
    '<p class="qwe rty" id="qwerty"><strong>Para</strong>graph</p>'.should have_tag('p.qwe.rty strong')
    # or you can use another syntax for examples above
    '<p class="qwe rty" id="qwerty"><strong>Para</strong>graph</p>'.should have_tag('p') do
      with_tag('strong')
    end
    '<p class="qwe rty" id="qwerty"><strong>Para</strong>graph</p>'.should have_tag('p#qwerty') do
      with_tag('strong')
    end
    '<p class="qwe rty" id="qwerty"><strong>Para</strong>graph</p>'.should have_tag('p.qwe.rty') do
      with_tag('strong')
    end
  • special case for classes matching:

    # all of this are equivalent:
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p', :with => { :class => 'qwe rty' })
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p', :with => { :class => 'rty qwe' })
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p', :with => { :class => ['rty', 'qwe'] })
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p', :with => { :class => ['qwe', 'rty'] })

    The same works with :without:

    # all of this are equivalent:
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p', :without => { :class => 'qwe rty' })
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p', :without => { :class => 'rty qwe' })
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p', :without => { :class => ['rty', 'qwe'] })
    '<p class="qwe rty" id="qwerty">Paragraph</p>'.should have_tag('p', :without => { :class => ['qwe', 'rty'] })
  • content matching:

    '<p> Some content&nbsphere</p>'.should have_tag('p', :text => ' Some content here')
    # or
    '<p> Some content&nbsphere</p>'.should have_tag('p') do
      with_text ' Some content here'
    end
    
    '<p> Some content&nbsphere</p>'.should have_tag('p', :text => /Some content here/)
    # or
    '<p> Some content&nbsphere</p>'.should have_tag('p') do
      with_text /Some content here/
    end
    
    # mymock.text == 'Some content here'
    '<p> Some content&nbsphere</p>'.should have_tag('p', :content => mymock.text)
    # or
    '<p> Some content&nbsphere</p>'.should have_tag('p') do
      with_content mymock.text
    end
  • usage with capybara and cucumber:

    page.should have_tag( ... )

where page is an instance of Capybara::Session

and of course you can use the without_ matchers (see the documentation).

More info

You can find more on RubyDoc, take a look at have_tag method.

Also, please read CHANGELOG, it might be helpful.

Contribution

  1. Fork the repository
  2. Add tests for your feature
  3. Write the code
  4. Add documentation for your contribution
  5. Send a pull request

Contributors

MIT Licensed

Copyright (c) 2011-2012 Dmitrij Mjakotnyi

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.

rspec-html-matchers's People

Contributors

cemeng avatar cimm avatar empact avatar kellyfelkins avatar kucaahbe avatar manuelmeurer avatar rwilcox avatar watsonbox avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.