加载包含硒有用的测试功能的外部JS文件 [英] Load an external js file containing useful test functions in selenium
问题描述
在硒的runScript命令真的很有用,而且我用它来总值的表,然后存储这样的值
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>
在理想情况下,我想能够指向外部的js文件,而不是在命令作为一个字符串的JavaScript,这样我就可以在某些测试功能加载和使用storeEval来获取函数的返回
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
因此,我们不得不
<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);
}
关于它的思考插件增加了一个loadScript动作用于检查外部js文件,然后通过文件内容的runScript会做这项工作。但我并不想另起炉灶,我从来没有在以前建造的插头。
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.
推荐答案
的的runScript
命令只是增加了一个&LT; SCRIPT&GT;
包含脚本到DOM,让浏览器中运行它的元素。你可以做自己相同的,而不是在行脚本,使用 SRC =
属性告诉浏览器加载哪些文件。您可能需要加载从Web服务器上的文件,因为一些浏览器将不允许从网上下载页面访问文件:
网址
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.
这篇关于加载包含硒有用的测试功能的外部JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!