2

在硒的runScript中命令是真正有用的,並且我使用它來合計值在表中,然後存儲這樣負載含有硒有用的測試功能的外部js文件

<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會做這項工作。但我不想重新發明輪子,我以前從來沒有建立過插件。

回答

2

runScript命令僅僅將包含腳本的<SCRIPT>元素添加到DOM,並讓瀏覽器運行它。您可以自己做同樣的事情,而不是使用內嵌腳本,請使用SRC=屬性告訴瀏覽器要加載的文件。您可能必須從Web服務器加載文件,因爲某些瀏覽器不允許從網絡加載的頁面訪問file: URL。

+0

是否在我正在測試的頁面上添加腳本標記? – chim

+1

不,我的意思是做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); –

+0

感謝這個Ross,我還沒有測試過。你的意思是我使用storeEval在IDE中運行這個JavaScript,還是你有不同的想法? – chim