Forum Discussion

David91's avatar
David91
Frequent Contributor
2 years ago

Put data to Table in KW variable - ReferenceError: Invalid left-hand side in assignment

Hello, 

 

please help. I need put data from script to table in KW variables . I try it with this.. 

KeywordTests.Test1.Variables.Var1.Mnoz(0) = QUANTITY;

 

But TestComplete after F5 return this message ReferenceError: Invalid left-hand side in assignment 10:19:27 Normal Error 0,00 0,00

 

I also try documentation code .. https://support.smartbear.com/testcomplete/docs/testing-with/variables/data-types/table.html#Creating

 

but its same problem .. 

 

function TableVarTest()

{

  // Create a table variable if it doesn't exist

  if(!Project.Variables.VariableExists("MyTable"))

    Project.Variables.AddVariable("MyTable", "Table");

  

  // Get a reference to the variable

  var t = Project.Variables.VariableByName("MyTable");

  

  // Add columns to the table

  t.AddColumn("Name");

  t.AddColumn("Age");

  t.AddColumn("City");

  

  // Create two rows

  t.RowCount = 2;

 

  // Fill in the table

  // The first row

  t.Name(0) = "John Smith";

  t.Age(0) = 24;

  t.City(0) = "Seattle";

  

  // The second row (using the Item property)

  t.Item("Name", 1) = "Bob Feather";

  t.Item("Age", 1) = 59;

  t.Item("City", 1) = "Milltown";

}

 

Please for advice, how put data to table from script to KW table variable. 

 

Thank you

  • KB1's avatar
    KB1
    Champion Level 2

    Hello,

     

    To assign data to keyword test variables, you should use the appropriate TestComplete keyword operations rather than direct JavaScript assignments. Here's the correct approach to populate a keyword test table variable:

    1. Create a keyword test and define a table variable in it. Let's assume the table variable is named "MyTable".
    2. In your script, you can access the keyword test and the table variable as follows:
    // Get a reference to the keyword test
    var keywordTest = KeywordTests.Test1; // Replace "Test1" with the actual name of your keyword test
    
    // Assign data to the table variable
    keywordTest.Variables.MyTable.RowCount = 2; // Number of rows in the table
    
    // The first row
    keywordTest.Variables.MyTable.Name(0) = "John Smith";
    keywordTest.Variables.MyTable.Age(0) = 24;
    keywordTest.Variables.MyTable.City(0) = "Seattle";
    
    // The second row
    keywordTest.Variables.MyTable.Name(1) = "Bob Feather";
    keywordTest.Variables.MyTable.Age(1) = 59;
    keywordTest.Variables.MyTable.City(1) = "Milltown";
    

     

    Please ensure that the keyword test variable "MyTable" is properly defined in your keyword test before running the script.

     

    By using this approach, you'll be able to populate the keyword test table variable with the desired data. Remember to replace "Test1" with the actual name of your keyword test.

    I hope this helps! If you have any further questions or issues, please feel free to ask.

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Use the $set method to change the property value of item for type Table. Example shows before and after property value change

     

    • David91's avatar
      David91
      Frequent Contributor

      thanks, how to write it to the console like this.. but not save it to the table.. 

       

      I would need to store the value in a cell so that I can use it in the next phase of the test

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    During runtime, you can change the value and the value will remain the same, until it's stopped. If you want the value to remain the same you can use project persistent variables. However, you can only use project temporary variable for type Table.

     

    See Variables Page for more information.