Utilizing the runScript command in selenium has proven to be incredibly helpful for me. I've been using it to calculate values within a table and then store the result like so:
<tr>
<td>runScript</td>
<td>var cumulative = 0.0; $('table.quote-review-group-component').eq(0).find('tr').each( function( i,el ){var singleStackTotal = $(el).find('td').eq(4).html();if( singleStackTotal ){cumulative += parseFloat( singleStackTotal.substring(1) );} }); cumulative = cumulative.toFixed(2)</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>selenium.browserbot.getUserWindow().cumulative</td>
<td>cumulative</td>
</tr>
<tr>
<td>echo</td>
<td>${cumulative}</td>
<td></td>
</tr>
<tr>
<td>verifyEquals</td>
<td>£${cumulative}</td>
<td>${total}</td>
</tr>
In an ideal scenario, I would prefer to reference an external js file rather than embedding the JavaScript within the command as a string. This would allow me to load test functions and utilize storeEval to retrieve the function's return value.
This approach would involve:
<tr>
<td>runExternalScript</td>
<td>/path/to/external/extra-tests.js</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>selenium.browserbot.getUserWindow().getCumulative(0)</td>
<td>cumulative0</td>
</tr>
<tr>
<td>verifyEquals</td>
<td>£${cumulative}</td>
<td>${total}</td>
</tr>
The contents of the external script file would resemble this structure:
function checkSingleGroupListTotal( index ){
if ( index == "undefined" ){
index = 0;
}
var cumulative = 0.0;
$('table.quote-review-group-component').eq(index).find('tr').each( function( i,el ){
var singleStackTotal = $(el).find('td').eq(4).html();
if( singleStackTotal ){
cumulative += parseFloat( singleStackTotal.substring(1) );
}
});
return cumulative.toFixed(2);
}
An alternative solution could involve creating a plugin that introduces a loadScript action. This action would verify the presence of an external js file and then pass the file contents to the runScript command. While this idea seems promising, I am hesitant to embark on building a plugin as I have no prior experience in doing so.