The runScript command in selenium is really useful, and I'm using it to total values in a table and then store the value like this
<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>
Ideally I'd like to be able to point to an external js file rather than have the javascript in the command as a string, so that I can load in some test functions and use storeEval to get the return of the function
So we'd have
<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>
And the external script would look like this
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);
}
Thinking about it a plugin which adds a loadScript action which checks for the external js file and then passes the file contents to runScript would do the job. But I don't want to reinvent the wheel, and I've never built a plug in before.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…