Brain storming a method of validating a HTML table's content with Watir-webdriver
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="#" clas开发者_JAVA技巧s="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>
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
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
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
精彩评论