【问题标题】:Load an external js file containing useful test functions in selenium在 selenium 中加载包含有用测试功能的外部 js 文件
【发布时间】:2011-10-06 14:08:24
【问题描述】:

selenium 中的 runScript 命令真的很有用,我用它来汇总表中的值,然后像这样存储值

<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>

理想情况下,我希望能够指向外部 js 文件,而不是将命令中的 javascript 作为字符串,这样我就可以加载一些测试函数并使用 storeEval 来获取函数的返回

所以我们有

<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>

外部脚本看起来像这样

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);
}

考虑一个插件,它添加一个 loadScript 动作来检查外部 js 文件,然后将文件内容传递给 runScript 就可以完成这项工作。但我不想重新发明轮子,而且我之前从未构建过插件。

【问题讨论】:

    标签: javascript selenium automated-tests selenium-rc selenium-ide


    【解决方案1】:

    runScript 命令只是将包含脚本的&lt;SCRIPT&gt; 元素添加到 DOM 并让浏览器运行它。您可以自己执行相同的操作,而不是使用内联脚本,而是使用 SRC= 属性告诉浏览器要加载什么文件。您可能必须从 Web 服务器加载文件,因为某些浏览器不允许从网络加载的页面访问 file: URL。

    【讨论】:

    • 你的意思是在我正在测试的页面上添加一个脚本标签吗?
    • 不,我的意思是完全按照 Selenium 所做的,但有一点不同: var doc = this.browserbot.getCurrentWindow().document; var scriptTag = doc.createElement("script"); scriptTag.type = "text/javascript" scriptTag.src = 'your_script_URL_goes_here'; doc.body.appendChild(scriptTag);
    • 感谢罗斯,我还没有测试过。你的意思是我使用 storeEval 在 IDE 中运行这个 javascript,还是你有不同的想法?
    猜你喜欢
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    相关资源
    最近更新 更多