Plugins - Assert Select

StarAdd to favorites

assert_select helps you write functional tests using CSS selectors.

If you’re using HTML and CSS, you’re already familiar with CSS selectors. CSS selectors have a simple syntax that lets you pick parts of the page and apply styling. assert_select uses the same simple syntax, for testing the content of the page.

You can test that selected element(s) exist, has specific text content, test number and order of elements, and a few more tricks.

Here’s a simple example that asserts the page has the right title:

  det test_page_has_right_title
    get :index
    assert_select "title", "Welcome"
  end

Here’s a few more examples:

  # Form includes four input fields
  assert_select "form input", 4

  # Page does not have any forms in it.
  assert_select "form", false, "Page must contain no forms"

  # Page has one link back to user's page.
  assert_select "a[href=?]", url_for(:controller=>"user", :id=>user_id),
                :count=>1, :text=>"Back to page"

  assert_select “form[action=http://test.host/login]” do
    assert_select “input[name=username]”
    assert_select “input[name=password]”
  end

  assert_select "div#notice", flash[:notice] || false

Assaf Arkin

http://blog.labnotes.org/2006/07/03/assert_select-plugin-for-rails/

http://labnotes.org/svn/public/ruby/rails_plugins/assert_select/

OpenSource

  • Currently 4.1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Testing

Tags

Comments

Add a comment
ryan 10 Jan 2007

assert_select is teh bomb and is in rails 1.2 :)

iain 29 Oct 2006

assert_tag's documentation lists as the most interesting use:

assert_tag :tag => "div", :ancestor => { :tag => "ul" }, :parent => { :tag => "li", :attributes => { :class => "enum" } }, :descendant => { :tag => "span", :child => /hello world/ }

which is equivalent to:

assert_select "ul li.enum > div span", /hello world/

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?