集思广益,讨论使用 Watir-webdriver 验证 HTML 表内容的方法

发布于 2024-11-09 20:14:00 字数 3967 浏览 4 评论 0原文

我正在尝试找出一种方法来使用 Watir-webdriver 检查 HTML 表的内容。基本上我想根据保存的有效表(CSV 文件)验证表内容,并且它们在刷新或重绘操作后是相同的。

到目前为止我想到的想法是:

  • 获取表 HTML 并将其作为字符串与基线值进行比较。
  • 迭代每个单元格并比较 HTML 或文本内容。
  • 生成表内容的二维数组表示并进行数组比较。

最快/最好的方法是什么?您对如何处理类似问题有自己的见解吗?

以下是该表的示例:

<table id="attr-table">
<thead>
<tr><th id="attr-action-col"><input type="checkbox" id="attr-action-col_box" class="attr-action-box" value=""></th><th id="attr-scope-col"></th><th id="attr-workflow-col">Status</th><th id="attr-type-col"></th><th id="attr-name-col">Name<span class="ui-icon ui-icon-triangle-1-n"></span></th><th id="attr-value-col">Francais Value</th></tr></thead>
<tbody>
<tr id="attr-row-209"><td id="attr_action_209" class="attr-action-col"><input type="checkbox" id="attr_action_209_box" class="attr-action-box" value=""></td><td id="attr_scope_209" class="attr-scope-col"><a href="#" class="ws-invoke-editor" id="attr_scope_209_a"><img src="images/attrib_bullet_global.png" title="global"></a></td><td id="attr_workflow_209" class="attr-workflow-col"></td><td id="attr_type_209" class="attr-type-col"><a href="#" class="ws-invoke-editor" id="attr_type_209_a"><img src="images/attrib_text.png"></a></td><td id="attr_name_209" class="attr-name-col"><a href="#" class="ws-invoke-editor" id="attr_name_209_a">Name of: Catalogue</a></td><td id="attr_value_209" class="attr-value-col"><a href="#" class="ws-invoke-editor lang_10" id="attr_value_209_a"><p class="acms ws-editable-content lang_10">2010 EI-176</p></a></td></tr>
<tr id="attr-row-316"><td id="attr_action_316" class="attr-action-col"><input type="checkbox" id="attr_action_316_box" class="attr-action-box" value=""></td><td id="attr_scope_316" class="attr-scope-col"><a href="#" class="ws-invoke-editor" id="attr_scope_316_a"><img src="images/attrib_bullet_global.png" title="global"></a></td><td id="attr_workflow_316" class="attr-workflow-col"></td><td id="attr_type_316" class="attr-type-col"><a href="#" class="ws-invoke-editor" id="attr_type_316_a"><img src="images/attrib_text.png"></a></td><td id="attr_name_316" class="attr-name-col"><a href="#" class="ws-invoke-editor" id="attr_name_316_a">_[Key] Media key</a></td><td id="attr_value_316" class="attr-value-col"><a href="#" class="ws-invoke-editor lang_10" id="attr_value_316_a"><p class="acms ws-editable-content lang_10"><span class="acms acms-choice" contenteditable="false" id="568">163</span></p></a></td></tr>
<tr id="attr-row-392"><td id="attr_action_392" class="attr-action-col"><input type="checkbox" id="attr_action_392_box" class="attr-action-box" value=""></td><td id="attr_scope_392" class="attr-scope-col"><a href="#" class="ws-invoke-editor" id="attr_scope_392_a"><img src="images/attrib_bullet_global.png" title="global"></a></td><td id="attr_workflow_392" class="attr-workflow-col"></td><td id="attr_type_392" class="attr-type-col"><a href="#" class="ws-invoke-editor" id="attr_type_392_a"><img src="images/attrib_numeric.png"></a></td><td id="attr_name_392" class="attr-name-col"><a href="#" class="ws-invoke-editor" id="attr_name_392_a">_[Key] Numéro d'ordre</a></td><td id="attr_value_392" class="attr-value-col"><a href="#" class="ws-invoke-editor lang_10" id="attr_value_392_a"><p class="acms ws-editable-content lang_10">2</p></a></td></tr>
</tbody>
</table>

I am trying to work out a method to check the content of an HTML table with Watir-webdriver. Basically I want to validate the table contents against a saved valid table (CSV file) and they are the same after a refresh or redraw action.

Ideas I've come up with so far are to:

  • Grab the table HTML and compare that as a string with the baseline value.
  • Iterate through each cell and compare the HTML or text content.
  • Generate a 2D array representation on the table contents and do an array compare.

What would be the fastest/best approach? Do you have insights on how you handled a similar problem?

Here is an example of the table:

<table id="attr-table">
<thead>
<tr><th id="attr-action-col"><input type="checkbox" id="attr-action-col_box" class="attr-action-box" value=""></th><th id="attr-scope-col"></th><th id="attr-workflow-col">Status</th><th id="attr-type-col"></th><th id="attr-name-col">Name<span class="ui-icon ui-icon-triangle-1-n"></span></th><th id="attr-value-col">Francais Value</th></tr></thead>
<tbody>
<tr id="attr-row-209"><td id="attr_action_209" class="attr-action-col"><input type="checkbox" id="attr_action_209_box" class="attr-action-box" value=""></td><td id="attr_scope_209" class="attr-scope-col"><a href="#" class="ws-invoke-editor" id="attr_scope_209_a"><img src="images/attrib_bullet_global.png" title="global"></a></td><td id="attr_workflow_209" class="attr-workflow-col"></td><td id="attr_type_209" class="attr-type-col"><a href="#" class="ws-invoke-editor" id="attr_type_209_a"><img src="images/attrib_text.png"></a></td><td id="attr_name_209" class="attr-name-col"><a href="#" class="ws-invoke-editor" id="attr_name_209_a">Name of: Catalogue</a></td><td id="attr_value_209" class="attr-value-col"><a href="#" class="ws-invoke-editor lang_10" id="attr_value_209_a"><p class="acms ws-editable-content lang_10">2010 EI-176</p></a></td></tr>
<tr id="attr-row-316"><td id="attr_action_316" class="attr-action-col"><input type="checkbox" id="attr_action_316_box" class="attr-action-box" value=""></td><td id="attr_scope_316" class="attr-scope-col"><a href="#" class="ws-invoke-editor" id="attr_scope_316_a"><img src="images/attrib_bullet_global.png" title="global"></a></td><td id="attr_workflow_316" class="attr-workflow-col"></td><td id="attr_type_316" class="attr-type-col"><a href="#" class="ws-invoke-editor" id="attr_type_316_a"><img src="images/attrib_text.png"></a></td><td id="attr_name_316" class="attr-name-col"><a href="#" class="ws-invoke-editor" id="attr_name_316_a">_[Key] Media key</a></td><td id="attr_value_316" class="attr-value-col"><a href="#" class="ws-invoke-editor lang_10" id="attr_value_316_a"><p class="acms ws-editable-content lang_10"><span class="acms acms-choice" contenteditable="false" id="568">163</span></p></a></td></tr>
<tr id="attr-row-392"><td id="attr_action_392" class="attr-action-col"><input type="checkbox" id="attr_action_392_box" class="attr-action-box" value=""></td><td id="attr_scope_392" class="attr-scope-col"><a href="#" class="ws-invoke-editor" id="attr_scope_392_a"><img src="images/attrib_bullet_global.png" title="global"></a></td><td id="attr_workflow_392" class="attr-workflow-col"></td><td id="attr_type_392" class="attr-type-col"><a href="#" class="ws-invoke-editor" id="attr_type_392_a"><img src="images/attrib_numeric.png"></a></td><td id="attr_name_392" class="attr-name-col"><a href="#" class="ws-invoke-editor" id="attr_name_392_a">_[Key] Numéro d'ordre</a></td><td id="attr_value_392" class="attr-value-col"><a href="#" class="ws-invoke-editor lang_10" id="attr_value_392_a"><p class="acms ws-editable-content lang_10">2</p></a></td></tr>
</tbody>
</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖哒 2024-11-16 20:14:00

这只是我想到的一个想法。我使用哈希和类对象而不是二维数组。

foo.csv

209,global,text.Catalogue,2010 EI-176
392,global,numeric,Numéro d'ordre,2

require 'csv'
expected_datas = CSV.readlines('foo.csv').map do |row|
  {
    :id => row[0],
    :scope => row[1],
    :type => row[2],
    :name => row[3],
    :value => row[4]
  }
end

class Data
  attr_reader :id,:scope,:type,:name,:value
  def initialize(tr)
    id = tr.id.slice(/attr-row-([0-9]+)/,1)
    scope = tr.td(:id,/scope/).img.src.slice(/attr_bullet_(.+?).png/,1)
    type = tr.td(:id,/type/).img.src.slice(/attrib_(.+?).png/,1)
    name = tr.td(:id,/name/).text
    value = tr.td(:id,/value/).text
  end
end

browser = Watir::Browser.new
browser.goto 'foobar'
datas = browser.table(:id,'attr-table').tbody.trs.map{|tr| Data.new(tr)}

datas.zip(expected_datas).each do |data,expected_data|
  Data.instance_methods(false).each do |method|
    data.send(method).should == expected_data[method.to_sym]
  end
end

# something action (refresh or redraw action)
browser.refresh

after_datas = browser.table(:id,'attr-table').tbody.trs.map{|tr| Data.new(tr)}

datas.zip(after_datas).each do |data,after_data|
  Data.instance_methods(false).each do |method|
    data.send(method).should == after_data.send(method)
  end
end

Just one idea I came up with. I used Hash and Class object instead of 2D array.

foo.csv

209,global,text.Catalogue,2010 EI-176
392,global,numeric,Numéro d'ordre,2

require 'csv'
expected_datas = CSV.readlines('foo.csv').map do |row|
  {
    :id => row[0],
    :scope => row[1],
    :type => row[2],
    :name => row[3],
    :value => row[4]
  }
end

class Data
  attr_reader :id,:scope,:type,:name,:value
  def initialize(tr)
    id = tr.id.slice(/attr-row-([0-9]+)/,1)
    scope = tr.td(:id,/scope/).img.src.slice(/attr_bullet_(.+?).png/,1)
    type = tr.td(:id,/type/).img.src.slice(/attrib_(.+?).png/,1)
    name = tr.td(:id,/name/).text
    value = tr.td(:id,/value/).text
  end
end

browser = Watir::Browser.new
browser.goto 'foobar'
datas = browser.table(:id,'attr-table').tbody.trs.map{|tr| Data.new(tr)}

datas.zip(expected_datas).each do |data,expected_data|
  Data.instance_methods(false).each do |method|
    data.send(method).should == expected_data[method.to_sym]
  end
end

# something action (refresh or redraw action)
browser.refresh

after_datas = browser.table(:id,'attr-table').tbody.trs.map{|tr| Data.new(tr)}

datas.zip(after_datas).each do |data,after_data|
  Data.instance_methods(false).each do |method|
    data.send(method).should == after_data.send(method)
  end
end
半葬歌 2024-11-16 20:14:00

您希望报告不匹配的详细程度如何?我认为这可能很好地定义了您想要采取的方法。

例如,如果您只想知道是否存在不匹配,而不关心哪里不匹配,那么比较数组可能是最简单的。

如果行的顺序可能会有所不同,那么我认为比较哈希可能是最好的

如果您希望单独报告每个不匹配,那么按行和列迭代将允许您报告离散错误,特别是如果您构建差异列表然后执行您的操作根据发现的差异数量在最后断言

What level of detail do you want the mismatch(es) reported with? I think that might well define the approach you want to take.

For example if you just want to know if there's a mismatch, and don't care where, then comparing arrays might be easiest.

If the order of the rows could vary, then I think comparing Hashes might be best

If you want each mismatch reported individually then iterating by row and column would allow you to report discrete errors, especially if you build a list of differences and then do your assert at the very end based on number of differences found

十年不长 2024-11-16 20:14:00

您可以选择完全匹配

before_htmltable <=> after_htmltable

或者你可以去掉空格

before_htmltable.gsub(/\s+/, ' ') <=> after_htmltable.gsub(/\s+/, ' ')

我认为创建数组然后比较每个元素会更昂贵。

戴夫

You could go for exact match

before_htmltable <=> after_htmltable

Or you could strip whitespace

before_htmltable.gsub(/\s+/, ' ') <=> after_htmltable.gsub(/\s+/, ' ')

I would think that creating the array then comparing each element would be more expensive.

Dave

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文