GithubHelp home page GithubHelp logo

hiteshsharma / rspec-html-matchers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kucaahbe/rspec-html-matchers

0.0 1.0 0.0 367 KB

Old school have_tag, with_tag(and more) matchers for rspec 3 (Nokogiri powered)

Home Page: http://rubygems.org/gems/rspec-html-matchers

License: MIT License

Ruby 99.10% Gherkin 0.90%

rspec-html-matchers's Introduction

rspec-html-matchers

RSpec 3 matchers for testing your html (for RSpec 2 use 0.5.x version).

Gem Version Build Status

Goals

Install

Add to your Gemfile in the :test group:

gem 'rspec-html-matchers'

Include it in your RSpec configuration:

RSpec.configure do |config|
  config.include RSpecHtmlMatchers
end

or just in your spec(s):

describe "my view spec" do
  include RSpecHtmlMatchers

  it "has tags" do
    expect(rendered).to have_tag('div')
  end

end

Cucumber configuration:

World RSpecHtmlMatchers

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

Usage

so perhaps 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 the following:

expect(rendered).to 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 above should be self-descriptive, if not, please refer to the have_tag documentation

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

  • matching tags by css:

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

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

    The same works with :without:

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

    expect('<p> Some content&nbsphere</p>').to have_tag('p', :text => ' Some content here')
    # or
    expect('<p> Some content&nbsphere</p>').to have_tag('p') do
      with_text ' Some content here'
    end
    
    expect('<p> Some content&nbsphere</p>').to have_tag('p', :text => /Some content here/)
    # or
    expect('<p> Some content&nbsphere</p>').to have_tag('p') do
      with_text /Some content here/
    end
    
    # mymock.text == 'Some content here'
    expect('<p> Some content&nbsphere</p>').to have_tag('p', :text => mymock.text)
    # or
    expect('<p> Some content&nbsphere</p>').to have_tag('p') do
      with_text mymock.text
    end
    
    # matching text content as it's seen by user:
    rendered = <<HTML
    <p>
       content with ignored spaces around
    </p>
    HTML
    expect(rendered).to have_tag('p', :seen => 'content with ignored spaces around')
  • usage with capybara and cucumber:

    expect(page).to have_tag( ... )

where page is an instance of Capybara::Session

  • also included shorthand matchers for form inputs:

    • have_form
    • with_checkbox
    • with_email_field
    • with_file_field
    • with_hidden_field
    • with_option
    • with_password_field
    • with_radio_button
    • with_button
    • with_select
    • with_submit
    • with_text_area
    • with_text_field
    • with_url_field
    • with_number_field
    • with_range_field
    • with_date_field

and of course you can use the without_ matchers, for more info take a look at documentation

rspec 1 partial backwards compatibility:

you can match:

expect(response).to have_tag('div', 'expected content')
expect(response).to have_tag('div', /regexp matching expected content/)

RSpec 1 have_tag documentation

Matching Tag Attributes

You can also match the content of attributes by using selectors. For example, to ensure an img tag has an alt attribute, you can match:

expect(index).to have_tag("img[alt!='']")

More info

You can find more on documentation

Also, please read CHANGELOG and issues, 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

rspec-html-matchers's People

Contributors

kucaahbe avatar cimm avatar randoum avatar hlcfan avatar manuelmeurer avatar misaka avatar getaaron avatar empact avatar cemeng avatar grantneufeld avatar watsonbox avatar jeroenvisser101 avatar kellyfelkins avatar roooodcastro avatar rwilcox avatar

Watchers

hitesh sharma 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.