Agile Web Development

Build it. Launch it. Love it.

Assert Select

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

Vitals

Home http://blog.labnotes.org/2006/07/03/assert_select-plugin-for-rails/
Repository http://labnotes.org/svn/public/ruby/rails_plugins/assert_select/
License OpenSource
Rating (10 votes)
Owner Assaf Arkin
Created 9 July 2006

Comments

  • Avatar
    iain
    29 October 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/

  • ryan
    10 January 2007

    assert_select is teh bomb and is in rails 1.2 :)

Add a comment