Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

javascript - Load an external js file containing useful test functions in selenium

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...