开发者

Load an external js file containing useful test functions in selenium

开发者 https://www.devze.com 2023-04-11 18:28 出处:网络
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

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>开发者_C百科${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.


The runScript command merely adds a <SCRIPT> element containing the script to the DOM and lets the browser run it. You can do the same yourself, and instead of an in-line script, use the SRC= attribute to tell the browser what file to load. You may have to load the file from a web server, because some browsers won't allow page loaded from the net to access a file: URL.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号