百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rspec-html-matchers

> 测试质量
开源

老版本的 have_tag、with_tag(以及更多)匹配器用于 rspec 3(由 Nokogiri 提供支持)

197 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

老版本的 have_tag、with_tag(以及更多)匹配器用于 rspec 3(由 Nokogiri 提供支持)

rspec-html-matchers

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

  • designed for testing complex html output. If you plan to perform simple matching, consider using:
    • assert_select
    • matchers provided out of the box in rspec-rails
    • matchers provided by capybara
  • developer-friendly output in error messages
  • built on top of nokogiri
  • has support for capybara, see below
  • syntax is similar to have_tag matcher from rspec-rails 1.x, but with own syntactic sugar
  • framework agnostic, as input should be String (or capybara's page, see below)

Install

Add to your Gemfile in the :test group:

ruby
gem 'rspec-html-matchers'

Include it in your RSpec configuration:

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

or just in your spec(s):

ruby
describe "my view spec" do
  include RSpecHtmlMatchers

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

Cucumber configuration:

ruby
World RSpecHtmlMatchers

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

Usage

so perhaps your code produces following output:

xml

Simple Form

  

  

so you test it with the following:

ruby
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:
…
  • special case for classes matching:

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

    The same works with :without:

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

…
  • usage with capybara and cucumber:

    ruby
    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:

ruby
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:

ruby
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

  • Kelly Felkins
  • Ryan Wilcox
  • Simon Schoeters
  • Felix Tjandrawibawa
  • Szymon Przybył
  • Manuel Meurer
  • Andreas Riemer

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyrspecrubytesting

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类测试质量
定价开源

> 相关工具

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.