在selenium中加载一个包含有用测试函数的外部js文件
Load an external js file containing useful test functions in selenium
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来完成这项工作。但是我不想重新发明轮子,我以前从来没有做过插件。
runScript
命令只是将包含脚本的<SCRIPT>
元素添加到DOM中,并让浏览器运行它。您可以自己做同样的事情,而不是使用内联脚本,使用SRC=
属性来告诉浏览器加载什么文件。您可能必须从web服务器加载文件,因为一些浏览器不允许从网络加载的页面访问file:
URL。
相关文章:
- 为函数代码编写测试
- AngularJS指令单元测试中未定义的函数
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- 为什么不是'我的函数在解析云代码中工作吗?当我在Angular和Express中测试时,它是有效的
- 测试抛出异常的函数
- 如何使用console.log语句在Mocha中测试函数
- 是否可以测试javascript函数是否是构造函数
- 如何在JavaScript中对箭头函数进行单元测试
- Mocha/Chai测试链接到函数返回断言错误
- 如何在Javascript中测试函数的相等性
- jQuery-有没有一种方法可以测试函数是否异步
- 如何按照承诺使用mocha/chai/chai测试ES7异步函数
- 在http服务单元测试中使用request-js时,chai.request不是一个函数
- Jasmine:测试setTimeout函数会抛出一个错误
- Mocking console.log()/MOCHA测试框架中的任何其他函数
- 如何使用console.log在因果报应测试中检查函数
- 使用 for 循环中的函数测试字符串时,我一直陷入无限循环
- 如何使用一个函数测试Angular控制器,该函数可以在不嘲笑AJAX调用的情况下进行AJAX调用
- 函数测试一系列值中的值和返回值
- 是否有任何QUnit断言/函数测试数组中的元素