NEED NEW POST UPDATES VIA EMAIL? SUBSCRIBE HERE

Enter your email address:

Delivered by FeedBurner

Sunday, 14 July 2013

Example of "while" and "endWhile" Loop Commands In Selenium IDE With Use Of User Extension

How to use "while" and "endWhile" command in selenium IDE
"while" command is not supported by default in selenium IDE. Selenium IDE also not supporting any conditional(if condition) commands by default. To get support of "while" loop as a Advanced Selenium IDE feature, You need to attach user extension with selenium IDE. You can read my post about how to attach user extension with selenium IDE.

I shared user extension file for "while" loop command. Click here to download selenium IDE user extension file for "while" and "endWhile" Commands and save it in your hard drive(only in .js format)
with name "user-extension.js" and attach it with selenium IDE as shown in bellow figure.


After attaching selenium IDE user extension file, You need to restart your selenium IDE window to get its effect. Once you restart your selenium IDE, you are ready to use "while" and "endWhile" command with selenium IDE. Copy-paste bellow given example script for "while" command in your selenium IDE and run it to view how it works.

New Test
CommandTargetValue
openhttp://docs.seleniumhq.org/
setSpeed1000
store1MyVar
whilestoredVars.MyVar <= 3
echo${MyVar}
highlightcss=img[alt="Selenium Logo"]
storejavascript{storedVars.MyVar++;}
endWhile


(Note : You can look at this selenium IDE tutorial posts to be master in just 7 days).

In above example, look at command execution sequence carefully. "while" loop will be rotated 3 times and execute all inner(commands between "while" and "endWhile") commands for 3 times. Here syntax, "storedVars.MyVar <= 3" with "while" command will check value of "MyVar" every time and keep running until it becomes <=3.  Inner javascript ("javascript{storedVars.MyVar++;}") will increase the value of variable "MyVar" every time by 1 which was set to 1 initially. Above script will high lite selenium logo on selenium site for 3 times.

15 comments:

  1. Can you please tell me how to break while loop in selenium ide?

    Something like EXIT or BREAK.

    Thanks

    Kashyap

    ReplyDelete
    Replies
    1. You can use "gotoif" command to jump at the end of script when your condition match. You can see how to use "gotoif" command with selenium IDE at http://software-testing-tutorials-automation.blogspot.in/search/label/gotoIf%20Command

      Delete
  2. While and End while not here in Selenium Ide command.please check and explain.
    I have run this script but not working properly. I have save the js file and restart the selenium also.

    ReplyDelete
    Replies
    1. Give me your email id here. Let me send to script for you.

      Delete
    2. Your email id is not valid. It shows me message "Delivery to the following recipient failed permanently" when trying to send email. Please give me other id if you have.

      Delete
  3. I have run this script but not working properly. I have save the js file and restart the selenium also.

    ReplyDelete
    Replies
    1. Its working for me. I think you have not configured properly.

      Delete
  4. I downloaded your user script as well as your rollup script. Then I created a while/endWhile script and turned it into a rollup. This worked fine. Then I created a second while/endWhile script and rolled that up too. Now I get an error when playing the first while/endWhile rollup in a test script. I am using Selenium IDE 2.4.0. See error below.

    [error] testCase.debugContext.currentCommand(...) is undefined
    [error] Unexpected Exception: TypeError: testCase.debugContext.currentCommand(...) is undefined. fileName -> chrome://selenium-ide/content/selenium-runner.js, lineNumber -> 242, columnNumber -> 6

    ReplyDelete
  5. same error ::::::::::::::::::::::::::::::
    [error] Unexpected Exception: TypeError: testCase.debugContext.currentCommand() is undefined. fileName -> chrome://selenium-ide/content/selenium-runner.js, lineNumber -> 242

    ReplyDelete
  6. [error] Unexpected Exception: SyntaxError: syntax error. fileName -> chrome://selenium-ide/content/tools.js -> file:///C:/My_docs/CRM_New/user-extension.js?1387406453042, lineNumber -> 91, columnNumber -> 0

    ReplyDelete
    Replies
    1. var gotoLabels= {};
      var whileLabels = {};

      // overload the original 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 = {};
      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 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 );
      }

      Delete
  7. the above example doesn't work.
    i'm using IDE 2.5.0 and have included your user-extension.js

    this did work:

    store | 1 | MyVar
    while | storedVars.MyVar <=10
    echo | ${MyVar}
    highlight | id=menu1
    storeEval | parseInt(storedVars.MyVar) +1 | MyVar
    endWhile

    ReplyDelete
  8. I actually figured out why this doesn't work for some and does for others. The problem is when you copy and paste the Target values.

    For example on the store javascript{storedVars.MyVar++;} value if you copy and paste a blank space into any of these values it will loop forever because variable name is not the same.

    Please trim all your spaces and this will work

    ReplyDelete
  9. Can you help me ?
    When i used while command.
    i got error ... unknown command :"while"

    ReplyDelete
  10. The loops works perfectly. But how can I continue with the next case after the loop ends ?

    I just want to loop a few cases within my test suite.

    ReplyDelete