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
|Created||9 July 2006|