Forum Discussion

pkudrys's avatar
pkudrys
Contributor
9 months ago

DBTableVariableObj.Reset() not working as expected

Hi guys, 

I'm experiencing a weird problem with DBTableVariableObj.Reset(), which is supposed to reset the iterator back to the first row. Well, it seems it does not reset the iterator at all :) 

Here is a simple function to demonstrate the issue...

//dbTableToRead is a DB Table variable diefined in keyword test
//columnToRead is a name of column
function testDBTableReset(dbTableToRead, columnToRead)
{
  var targetWord, i=1;
  
  // Obtains a DB Table variable
  var dbTable = dbTableToRead;
  
  // Initializes the iterator
  dbTable.Reset;
  
  Log.Message("Iteration - Start");

  // Iterate through rows
  while(!dbTable.IsEOF())
  {
      targetWord = dbTable.Value(columnToRead);
      Log.Message("Row number: " + i.toString() + " Value: " + targetWord);
      
    // Forward the iterator to the next row
    dbTable.Next();
    i++  
  }
  Log.Message("Iteration - End"); 
}

Basically, it should iterate over the items in DB Table variable and write them to log. I'm calling this function two times in the same keyword test...

And here is the result: 

Basically, the second run of the function jumps over the while(!dbTable.IsEOF()) block, as if the dbTable is not reset? But it should be or am I doing something wrong? 

It seems it's not possible to attach the sample project here, so if you are interested, you can get it here (zipped and inspected for viruses):
https://drive.google.com/file/d/1TiRh50vKP6mguNRgvT13-vnyL-eP8rp1/view?usp=sharing

Thank you in advance for any suggestion.

  • What you are seeing is something called pass by value, as opposed to pass by reference. It means that JavaScript copies the values of the variables into the function arguments. 

    In this example, you would expect the value of myString to be changed to 'New String'. However, we are just passing the value,

    function myfunc(sameString) {
        sameString = 'New String';
    }
    
    function Test()
    {
        var myString = 'Old String';
        myfunc(myString);
        Log.Message(myString);
    }

    The same applies to what you have written. However, I would have expected Variables.testDBTable not to change at all, as you are "passing by value". But it seems like the object value is changing, as shown in this example,

    function increaseAge(obj) {
        obj.age += 1;
    }
    
    function Test()
    {
        let person = {
          name: 'SmartBear',
          age: 25,
        };
    
        increaseAge(person);
        Log.Message(person.age);
    }

    To reset your iterator, insert the following at the end of your function,

    KeywordTests.Test1.Variables.testDBTable.Reset();
  • rraghvani's avatar
    rraghvani
    Champion Level 3

    What you are seeing is something called pass by value, as opposed to pass by reference. It means that JavaScript copies the values of the variables into the function arguments. 

    In this example, you would expect the value of myString to be changed to 'New String'. However, we are just passing the value,

    function myfunc(sameString) {
        sameString = 'New String';
    }
    
    function Test()
    {
        var myString = 'Old String';
        myfunc(myString);
        Log.Message(myString);
    }

    The same applies to what you have written. However, I would have expected Variables.testDBTable not to change at all, as you are "passing by value". But it seems like the object value is changing, as shown in this example,

    function increaseAge(obj) {
        obj.age += 1;
    }
    
    function Test()
    {
        let person = {
          name: 'SmartBear',
          age: 25,
        };
    
        increaseAge(person);
        Log.Message(person.age);
    }

    To reset your iterator, insert the following at the end of your function,

    KeywordTests.Test1.Variables.testDBTable.Reset();
    • pkudrys's avatar
      pkudrys
      Contributor

      Hi rraghvani and thank you for the reply. The reset line you mentioned definitely helped! Thanks. 

      But it's really weird :) I mean, at start of the function, there is created a new variable dbTable, which is filled from the function's parameter dbTableToRead. And then the while loop is performed on that new variable, not on the original parameter, containing the DBTable variable. And because there is no error thrown on dbTable.Reset; row, one would expect the initialization of the dbTable iterator was successful? I still think there is something wrong with the reset? :)  Anyway, thank you for the solution. 

      • rraghvani's avatar
        rraghvani
        Champion Level 3

        I was almost about to give another explanations, and I've just noticed your statement dbTable.Reset; - you're missing the brackets. It should be dbTable.Reset();

        Now it should work, as you expect it too!