使用Selenium測試Web介面時使用迴圈控制功能

來源:互聯網
上載者:User

Selenium是一個很好用的Web介面測試架構。但它的功能也有不足之處,比如: 在Selenium IDE中不支援程式控制語句。下面介紹如何在Selenium IDE中添加程式控制功能。

1)下載Selenium外掛程式 (sideflow.js): 

如果不想下載,直接把下面的代碼儲存到本機也可。

var gotoLabels= {};var whileLabels = {};// overload the original Selenium reset functionSelenium.prototype.reset = function() {    // reset the labels    this.initialiseLabels();    // proceed with original reset code    this.defaultTimeout = Selenium.DEFAULT_TIMEOUT;    this.browserbot.selectWindow("null");    this.browserbot.resetPopups();}Selenium.prototype.initialiseLabels = function(){    gotoLabels = {};    whileLabels = { ends: {}, whiles: {} };    var command_rows = [];    var numCommands = testCase.commands.length;    for (var i = 0; i < numCommands; ++i) {        var x = testCase.commands[i];        command_rows.push(x);    }    var cycles = [];    for( var i = 0; i < command_rows.length; i++ ) {        if (command_rows[i].type == 'command')        switch( command_rows[i].command.toLowerCase() ) {            case "label":                gotoLabels[ command_rows[i].target ] = i;                break;            case "while":            case "endwhile":                cycles.push( [command_rows[i].command.toLowerCase(), i] )                break;        }    }      var i = 0;    while( cycles.length ) {        if( i >= cycles.length ) {            throw new Error( "non-matching while/endWhile found" );        }        switch( cycles[i][0] ) {            case "while":                if( ( i+1 < cycles.length ) && ( "endwhile" == cycles[i+1][0] ) ) {                    // pair found                    whileLabels.ends[ cycles[i+1][1] ] = cycles[i][1];                    whileLabels.whiles[ cycles[i][1] ] = cycles[i+1][1];                    cycles.splice( i, 2 );                    i = 0;                } else ++i;                break;            case "endwhile":                ++i;                break;        }    }}Selenium.prototype.continueFromRow = function( row_num ){    if(row_num == undefined || row_num == null || row_num < 0) {        throw new Error( "Invalid row_num specified." );    }    testCase.debugContext.debugIndex = row_num;}// do nothing. simple labelSelenium.prototype.doLabel = function(){};Selenium.prototype.doGotolabel = function( label ){    if( undefined == gotoLabels[label] ) {        throw new Error( "Specified label '" + label + "' is not found." );    }    this.continueFromRow( gotoLabels[ label ] );};Selenium.prototype.doGoto = Selenium.prototype.doGotolabel;Selenium.prototype.doGotoIf = function( condition, label ){    if( eval(condition) ) this.doGotolabel( label );}Selenium.prototype.doWhile = function( condition ){    if( !eval(condition) ) {        var last_row = testCase.debugContext.debugIndex;        var end_while_row = whileLabels.whiles[ last_row ];        if( undefined == end_while_row ) throw new Error( "Corresponding 'endWhile' is not found." );        this.continueFromRow( end_while_row );    }}Selenium.prototype.doEndWhile = function(){    var last_row = testCase.debugContext.debugIndex;    var while_row = whileLabels.ends[ last_row ] - 1;    if( undefined == while_row ) throw new Error( "Corresponding 'While' is not found." );    this.continueFromRow( while_row );}

: https://github.com/darrenderidder/sideflow

2)在Selenium中配置外掛程式。如所示,添加sideflow.js 到 "Selenium Core extensions (user-extensions.js)" 文字框中,重新啟動Selenium IDE。

3) 這樣就可以在Selenium中使用流程式控制制了,例如

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.