为空单元格验证 table

Validate table for empty cells

你能帮忙建议一些脚本来验证 table 单元格的空值吗?我需要验证 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

可能还有另一种检查空值的方法。

我不喜欢手动编写循环来遍历和验证每个单元格的一件事是您只能看到第一次失败的结果。如果有两个单元格是空白的,测试失败只会显示一个。

因此,我尝试使用内置的期望匹配器来检查每个元素(例如 all)。例如,以下获取每个单元格的文本长度并确保它至少有 1 个字符长。请注意,Watir 会去除 leading/trailing 个空格,因此长度 1 应该是一个实际字符。

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

失败的期望如下所示,包括每个失败的单元格:

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

使用页面对象 gem 会类似(方法略有不同)。假设 table 在页面中定义为 financials_grid:

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