How to put value in Table (Variables)
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 ?
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.
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
I have just spent hours looking for a way to do this.
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.
// Create a table variable if it doesn't exist
// Get a reference to the variable
var t = Project.Variables.VariableByName("MyTable");
// Add columns to the table
// 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;
var i = 0;
Log.AppendFolder("Row " + i++);
Log.Message("Name: " + itr.Value("Name"));
Log.Message("Age: " + itr.Value("Age"));
Log.Message("City: " + itr.Value("City"));
For more information, please see the Variables of the Table Type and TableVariable Object help topics.
I am not a member of the SmartBear Support Team, however I do attempt to help members of this community with my broad knowledge of SmartBear tools. My responses do not reflect an official SmartBear Support answer.
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"
If Not Project.Variables.VariableExists("MyTable") Then
Call Project.Variables.AddVariable("MyTable", "Table")
Set t = Project.Variables.VariableByName("MyTable")
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 ?
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.
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
Why automate? I do automated testing because there's only so much a human being can do and remain healthy. Sleep is a requirement. So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.
Mysterious Gremlin Master
Vegas Thrill Rider