Validate table for empty cells

524 views Asked by At

Could you help to advise some script to validate a table cells for empty values? I need to validate that there are no empty cells in table.

tableFG = page.table(:id => 'FinancialsGrid')
tableFG.rows.each do |row|
  row.cells.each do |cell|
    expect(cell.tableFG_element.text).should_not be_nil
  end
end

May be there is another way to check for empty values.

1

There are 1 answers

1
Justin Ko On BEST ANSWER

The one thing I do not like about manually writing a loop to iterate through and validate each cell is that you only see the result of the first failure. If there are say two cells that are blank, the test failure will only show one.

As a result, I try to make use of the built-in expectation matchers that check each element (ex all). For example, the following gets the length of text of each cell and makes sure that it is at least 1 character long. Note that Watir strips leading/trailing whitespaces, so a length of 1 should be an actual character.

financials_grid = browser.table(:id => 'FinancialsGrid')
expect(financials_grid.tds.map(&:text).map(&:length)).to all( be > 0 )

A failed expectation would look like the following and include each cell that fails:

expected [1, 0, 0, 1] to all be > 0

object at index 1 failed to match:
expected: > 0
got:   0

object at index 2 failed to match:
expected: > 0
got:   0

Using the page-object gem would be similar (with slightly different methods). Assuming the table is defined in the page as the financials_grid:

page = MyPage.new(browser)
expect(
    page.financials_grid_element.cell_elements.map(&:text).map(&:length)
).to all( be > 0 )