Forum Discussion

Lyna's avatar
Lyna
Occasional Contributor
14 years ago

How to put value in Table (Variables)

Hi,



I wanted to know if it's possible to put values in a Table (in the Variables Object) with a script. I don't want to have to work with the interface, it will be too long for me.



Is it possible ?



Thanks,


9 Replies

  • Hi,



    Since TestComplete 8.10 it's possible to add rows and columns to table variables and modify table cell values programmatically, during test execution. However, the changes you made within a table variable at runtime are not saved after the test execution is over since such variables are intended for temporary usage, not for permanent data storing. So, you can specify a table variable's structure, initialize its values from scripts and use them only during the current test run.



    You can create a table variable programmatically by using the Variables.AddVariable method. After obtaining a reference to the needed TableVariable object, you can add a new column with the desired name by calling the AddColumn method of this object. To change the number of rows in the table, simply specify the needed value in the RowCount property. To get and set values of table cells, you can use one of the following approaches:



       1) Call the Item property and specify the needed column and row in its parameters to get a reference to the needed table cell.

      

       2) For each column created in a table variable, the TableVariable object contains a special property with the name coinciding with the column name. So, you can obtain a particular cell value from the needed column by calling the property with the appropriate name and passing the needed row index via property parameters.



       3) By calling the Iterator property, you can obtain an iterator that lets you iterate through the table's rows and get or set values in the needed columns.



    Below is sample script code that demonstrates how you can create a table variable, initialize its cells and use the specified values during the test execution.





    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 three rows

      t.RowCount = 3;



      // Fill in the table



      // The first row

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

      t.Age(0) = 24;

      t.City(0) = "Seattle";



      // The second row

      t.Name(1) = "Susan McLaren";

      t.Age(1) = 35;

      t.City(1) = "Newcastle";



      // The third row (using the Item property)

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

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

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



      // Iterate through the table's rows

      var itr = t.Iterator;

      itr.Reset();

      var i = 0;

      while (!itr.isEOF()){

        Log.AppendFolder("Row " + i++);

        Log.Message("Name: " + itr.Value("Name"));

        Log.Message("Age: " + itr.Value("Age"));

        Log.Message("City: " + itr.Value("City"));

        Log.PopLogFolder();

        itr.Next();

       }

     return 0;

    }





    For more information, please see the Variables of the Table Type and TableVariable Object help topics.
    • ou_ryperd's avatar
      ou_ryperd
      Occasional Contributor

      Is it possible to retrieve a value from the table as can be done in VBScript with:

       

      set dict = CreateObject("Scripting.Dictionary")
      dict.add "Italy", "Rome"
      dict.add "Germany", "Berlin"

      msgbox dict.item("Italy") -> returns "Rome"

       

      e.g.

       

      Dim t
        If Not Project.Variables.VariableExists("MyTable") Then
          Call Project.Variables.AddVariable("MyTable", "Table")
        End If
        Set t = Project.Variables.VariableByName("MyTable")
        Call t.AddColumn("Name")
        Call t.AddColumn("Age")
        Call t.AddColumn("City")
        t.RowCount = 2
        t.Name(0) = "John Smith"
        t.Age(0) = 24
        t.City(0) = "Seattle"
        t.Item("Name", 1) = "Bob Feather"
        t.Item("Age", 1) = 59
        t.Item("City", 1) = "Milltown"
       
        msgbox t.item("City", "Name" = "Bob Feather") <- I want the City where the Name is Bob Feather

       

      How to do this ?

       

      • Ravik's avatar
        Ravik
        Super Contributor

        Wow. it's realy-realy very interesting. 

    • dennis1's avatar
      dennis1
      Occasional Contributor

      Is it still not possible to store data into a table (or another database) permanently to use it in other testcases?

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        You can use ADO objects built in to TestComplete to read and write from any database.  You can also write out to a file (text or excel) using various objects.

         

        So, the question is, what precisely do you need to do that you aren't able to?  We might be able to help with more detail.

  • Hi,



    At the moment, there is no way to add new items to a table variable and specify its structure from script - the only way to do this is to use TestComplete's GUI. We have an appropriate suggestion in our DB, and your post has increased its rating. Thank you.



    BTW, you can assign a new value to the needed table variable item from script by using the Item property.
  • bchadwick's avatar
    bchadwick
    New Contributor
    what is the syntax to use vbscript to insert a string value into a Table variable?  project.variable.<array_name>.item(i, j) = "Hi World"  doesn't work.  have tried other combinations to no avail.
  • bchadwick's avatar
    bchadwick
    New Contributor
    now I see, was setting elements in the array then checking the table in the Variables list (where the values aren't shown).  read through the table and the values entered into the table are saved, so good enough.
  • MulgraveTester's avatar
    MulgraveTester
    Frequent Contributor
    "At the moment, there is no way to add new items to a table variable and specify its structure from script "



    I have just spent hours looking for a way to do this.