【问题标题】:Validate table for empty cells验证空单元格的表格
【发布时间】:2015-01-12 13:29:26
【问题描述】:

您能否建议一些脚本来验证表格单元格的空值?我需要验证表格中没有空单元格。

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

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

【问题讨论】:

    标签: cucumber watir watir-webdriver page-object-gem rspec-expectations


    【解决方案1】:

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

    因此,我尝试使用内置的期望匹配器来检查每个元素(例如all)。例如,以下获取每个单元格的文本长度,并确保它至少为 1 个字符长。请注意,Watir 会去除前导/尾随空格,因此长度 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
    

    使用 page-object gem 将是相似的(方法略有不同)。假设该表在页面中定义为financials_grid

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

    【讨论】:

    • 太棒了。谢谢你,贾斯汀。它比我预期的要好。即使我有一个空单元格也已经很糟糕了。所以即使找到一个空单元格也不是那么糟糕,因为不应该没有一个空单元格。
    猜你喜欢
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多