Forum Discussion

william_roe's avatar
william_roe
Super Contributor
9 years ago
Solved

Loop Through Array Returned by Script in Keyword Test

I've written a large amount of script which use arrays to reduce the the data reads from the Excel spreadsheat and improve performance. I recently discovered that, unlike keyword tests, script references don't get renamed when objects are renamed and want to move this logic into keyword tests. However, I can't figure out how to interate through the array within the Keyword test. I'm storing the array in the Keyword a variable (AllCribs) and verified the values are correct (second image). However when attempting either a loop or forloop I'm receiving the exception 'The variable specified as the Loop Counter is of an invalid type. Expected types: Integer, Double'. What am I doing doing wrong?

 

Here's the script:

//
// Return list of ALL cribs
//
function ReadAllCribs() {
	var AllCribs = [];

	Project.Variables.Cribs.Reset();
	while (!Project.Variables.Cribs.IsEOF()) {
		AllCribs.push(Project.Variables.Cribs.Value("CribNumber"));

		Project.Variables.Cribs.Next();
	}
	return AllCribs;
}

 And here's the Keyword test

ForLoop.jpg

4 Replies

  • djadhav's avatar
    djadhav
    Regular Contributor

    In your For Loop statement in second screenshot, you have mentioned 'Variables.AllCribs' as your first parameter. According to this article (https://support.smartbear.com/viewarticle/72666/), the first parameter stands for the loop variable. This can only be an Interger or Double type, hence the error that you are getting.

     

    Example:

    for(i=0;i<=10;i++){

    print i;

    }

     

    In the above example the variable i is the loop variable. Now you see why you cannot have an array in the place of i. You can create another counter variable or even leave it blank. It'll get automatically created and deleted.

    • william_roe's avatar
      william_roe
      Super Contributor

      djadhav wrote:

      In your For Loop statement in second screenshot, you have mentioned 'Variables.AllCribs' as your first parameter. According to this article (https://support.smartbear.com/viewarticle/72666/), the first parameter stands for the loop variable. This can only be an Interger or Double type, hence the error that you are getting.

       

      Example:

      for(i=0;i<=10;i++){

      print i;

      }

       

      In the above example the variable i is the loop variable. Now you see why you cannot have an array in the place of i. You can create another counter variable or even leave it blank. It'll get automatically created and deleted.


      Thanks for the reply. What is the Keyword equivilant? I'm trying move away from script where possible for stated reasons. The only option for the first parameter 'Loop Variable' is a 'Variable'.

       

      Loop Variable.jpg