Thursday, August 18, 2011

Functional Testing in Rails(to test Html)

Hi all,

I have used listed out some uses of assert_tag in Rails functional test. In many case, we need to test the html content of page like checkbox is checked or not, text fields holds correct value, drop down selected with correct option. For those kind of needs, we need to go for assert_tag. Below are the example usage of assert_tag in functional testing.

In Rails html testing, assert_tag method plays an vital role. Following are some of the ways to use assert_tag method


Get links inside div

assert_select "div#divID" do
assert_select "a[href=?]", "/path/to/some/page"
end

Check the content of any html element(here is an example for Div)

by className
assert_select "div.className", "Page Heading or content inside the div"
by element ID
assert_select "div#elementId", "Page Heading or content inside the div"

Check how many times any DOM element appears on the page:

In the below example, we tested how many number of user images loaded on the page
assert_select "div.className table tr td.userImage img", {:count => 4}

Presence of Textbox:

assert_tag "input", :attributes => {:id => "user_screen_name", :size => "30", :type => "text"}

Get value of Dropdown:

option = css_select("select#elementId option[selected='selected']")[0]
assert_equal "public", option['value'].to_s

Code with testcase improves the quality of code and at the sametime reduces the errors. Keep writing test for each line of code.