問題描述
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>
外部腳本看起來像這樣
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
命令只是將包含腳本的 <SCRIPT>
元素添加到 DOM 并讓瀏覽器運行它.你可以自己做同樣的事情,而不是使用內嵌腳本,使用 SRC=
屬性來告訴瀏覽器要加載什么文件.您可能必須從 Web 服務器加載文件,因為某些瀏覽器不允許從網絡加載的頁面訪問 file:
URL.
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.
這篇關于在 selenium 中加載包含有用測試功能的外部 js 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!