Forum: Ruby on Rails RE: assert HTML table contents

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
mfuller (Guest)
on 2005-11-16 19:27
(Received via mailing list)
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"]
This topic is locked and can not be replied to.