RE: assert HTML table contents


#1

Since I didn’t get any response I quickly created my own:

I’m sure there is a better way, but seems to work for now (Have not put
it through any rigurous testing)

def get_table_tag(table_id)
{:tag => ‘table’, :attributes => {:id => table_id}}
end

def assert_table_row(table_id, row_number, expected_cell_contents)
tr_nodes = find_all_tag(:tag =>‘tr’, :parent =>
get_table_tag(table_id))
assert row_number < tr_nodes.size, “Table: #{table_id}. Looking for
row #{row_number}, but table only has #{tr_nodes.size} rows”
td_nodes = tr_nodes[row_number].find_all(:tag =>‘td’)
assert expected_cell_contents.size < td_nodes.size, “Table:
#{table_id}. Expecting at least #{expected_cell_contents.size} cells
however, row only has #{td_nodes.size}”

expected_cell_contents.each_with_index do |expected_cell_content, idx|
assert td_nodes[idx].find(:content => expected_cell_content),
“Unexpected cell contents. Table: #{table_id}. Row: #{row_number}.
Column: #{idx}. Expected: #{expected_cell_content}.”
end
end

example usage:
assert_table_row ‘table_name’, 0, [“row 0 col 0”, “row 0 col 1”, “row 0
col 2”]