Add Loop In Selenium Script

Here I am going to write the script  for online registration that will run two times for registration in http://www.specialtyansweringservice.net .

If you want you can increase the loop by increasing the loop number. Please follow the next step for detail information.

1)  For repeating the process again and again we need to add loop conditions. For this we need to download and add one plug-in called flow control to the selenium IDE.

 2) Please copy the following code and paste on notepad.

 

// Start

// **************************************************

 var gotoLabels= {};

var whileLabels = {};

var forLabels = {};

var forSteps=null;

var forParams=[];

 // overload the oritinal Selenium reset function

Selenium.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  = {};

    forLabels = { ends: {}, fors: {} };

    whileLabels = { ends: {}, whiles: {} };

        forSteps=null;

    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 cycles_while = [];

    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 “for”:

            case “next”:

                cycles_for.push( [command_rows[i].command.toLowerCase(), i] )

                break;           

                        case “while”:

            case “endwhile”:

                cycles_while.push( [command_rows[i].command.toLowerCase(), i] )

                break;

        }

    }

    var i = 0;

    while( cycles_for.length ) {

        if( i >= cycles_for.length ) {

            throw new Error( “non-matching for/next found” );

        }

        switch( cycles_for[i][0] ) {

            case “for”:

                 if(    ( i+1 < cycles_for.length )  && ( “next” == cycles_for[i+1][0] ) ) {

                     // pair found

                     forLabels.ends[ cycles_for[i+1][1] ] = cycles_for[i][1];

                     forLabels.fors[ cycles_for[i][1] ] = cycles_for[i+1][1];

                     cycles_for.splice( i, 2 );

                     i = 0;

                 } else ++i;

                 break;

             case “next”:

                 ++i;

                 break;

        }

    }

    var i = 0;

    while( cycles_while.length ) {

        if( i >= cycles_while.length ) {

            throw new Error( “non-matching while/endWhile found” );

        }

        switch( cycles_while[i][0] ) {

            case “while”:

                 if(    ( i+1 < cycles_while.length )  && ( “endwhile” == cycles_while[i+1][0] ) ) {

                     // pair found

                     whileLabels.ends[ cycles_while[i+1][1] ] = cycles_while[i][1];

                     whileLabels.whiles[ cycles_while[i][1] ] = cycles_while[i+1][1];

                     cycles_while.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.” + row_num); }

    testCase.debugContext.debugIndex = row_num;

}

 // do nothing. simple label

Selenium.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 );

}

 Selenium.prototype.doFor = function( init_max_inc, variable ) {

        if (forSteps==null) {

                var reg=new RegExp(“[;]+”, “g”);

                forParams=init_max_inc.split(reg);

                if(forParams.length != 3) throw new Error( “Paramètres incorrects: Initialisation;Maximum;Incrément” );     

                forSteps=Math.floor(Math.abs( (forParams[1]-forParams[0] )/forParams[2]));

                if (variable != ”) storedVars[variable]=forParams[0] ;

        }

    else if ( forSteps != 0 ) {

                forSteps–;

                if (variable != ”) storedVars[variable] = (+storedVars[variable]) + (+forParams[2]);

    }

        else {

                forSteps=null;

        var last_row = testCase.debugContext.debugIndex;

        var end_for_row = forLabels.fors[ last_row ];

        if( undefined == end_for_row ) throw new Error( “Corresponding ‘next’ is not found.” );

        this.continueFromRow( end_for_row );

        }

}

 Selenium.prototype.doNext = function() {

    var last_row = testCase.debugContext.debugIndex;

    var for_row = forLabels.ends[ last_row ] – 1;

    if( undefined == for_row ) throw new Error( “Corresponding ‘for’ is not found.” );

    this.continueFromRow( for_row );

}

 //*****************************************************

//End

 

3) Save the notepad as loop.js file.

 4) Open the selenium IDE by clicking Tools–> Selenium IDE in Firefox.

 5) Click on Options–>Options on selenium IDE.

6) Once you will click on the options the another window will open like-

7) Click on Browse option and browser the file loop.js

8) Click on Ok button and close the selenium window.

9) Again go to Browser Firefox–> Tools and open Selenium IDE.

Wow you have added successfully all the loops in selenium IDE.

Now we will record the script and add the loop to make the script repeatable. The same script will run again and again automatically as many times we want.

Please follow the instructions-

 

1) Open Firefox  browser and open selenium IDE. Make sure Selenium IDE is in recording mode. (By default it will be in recording mode)

 2) Type the url  http://www.specialtyansweringservice.net in browser and hit enter button.

3)   On the right side there is option Pricing. Click on it.

4)   Fill the fields (Company Name, First Name, Last Name, Email) .

5) Click on Submit Your Form button.

6) You are done with recording for submitting the form.

7) Once recording is done Selenium IDE will look like-

8) Save the script by pressing ctrl+s or file save. and click on stop button to stop the script.

9) Run and confirm that script is working fine. Before running make it slow.

 Now I am going to add loop so that the same registration process will repeat 3 times. For this we need to add few manual scripts. Its easy.

 10) Right click on “open” script and select option “Insert New Command”.

11) Similarly add three new commands on the top.

12) Click on the top blank scrip field add command as “store”, Target as “1” and Value as “y” .

Please check the screen shot-

13) Similarly in the second line add command as “store”, Target as “3” and Value as “x” .

14) Now in the third line add Command as “label”, Target as “target1” . Check the screen shot-

15) Click at the bottom of the script and add two “Insert New Command”.

16) At the first line in new command add command as “store”, Target as ${x}-1 , value as “x” .

 At the last I will explain why we are doing all these things.

 17) At the second line add command as “gotoIf” , Target as ”  ${y}<=${x} ” and value as ” target1″ .

18) Save the script by pressing ctrl+s or file–> save.

 Nice.. You are done with adding the loop.

Finally we are going to run the script and see the magic.

Make sure you are going to run the script in slow mode.

How Loop Works?

Here we are using four commands-

Command  Target               Value

store                  1                          y

store                 3                           x

label             target1

 (All the recorded Script)

 store                ${x}-1                 x

gotoIf        ${y}<=${x}      target1

Lets start with the first line. y is the variable where we are storing one value as 1.

x is another variable where we are storing value as 3. Because we want the script to run 3 times.

After that I have created one label as target1 because once first round of executing script will complete then for second round where the control should go to begin the script again.

 At the first executing we are reducing the value of x by one and making the condition satisfied at the last line. For ex-

The first run of script the value of ${x}-1 will be 3-1=2.

So the condition is getting satisfied as ${y}<=${x} = (1<=2). Here condition is true so loop will go to the target1

In the final round of executing the value of ${x}-1 will be 1-1=0.

So the condition is not getting satisfied as ${y}<=${x} = (1<=0). Here condition is false. So execution gets stopped.

 Similarly we can use another loops as our requirement will be.

 

 

<<Previous      Next >>

 

.