Ask a Question

Adding a dictionary object to a variable on keyword tests

New Contributor

Adding a dictionary object to a variable on keyword tests

Has anyone tried setting a variable in a keyword test with a value consisting of a dictionary object (a key value pair) ?


IT can be done on scripts but i never made it worked on KeywordTests


I tried using 

Set Variable varObj and then use code expression to put a value of 


but testcomplete says its not a valid expression. 


I need to put a dictionary object on the keyword test because i will be calling a method/function that accepts a dictionary as a parameter. This object contains a key value pair of headers and header values for use in sending an http requests.


Any leads on how i can achived this?

Esteemed Contributor

I've spent a little bit of time on it and I'm not sure that it's entirely possible to do EXACTLY what you want.


What I ended up doing was creating a script function whose only purpose was to define the dictionary and return it as the result


function defineDictionary(){
    var dict
    dict = {key1:"Value1", key2:"value2"}
    return dict

I then used the run script routine operation in the keyword test to run this.  Then, I used Set Variable to set a variable in the keyword test (type Object) to the Last Operation Result.  I could then access the key value pairs appropriately.


There's probably a way of doing the same with something like Run Code Snippet or something but I haven't figured it out yet.  This is one of the cases, I believe, where writing code in script may be the only way to do exactly what you want.

Robert Martin
[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
Extensions available

Set Variable Value operation can allow you to create dictionary variables inside of TestComplete.

Then using a run code snippet, you can access the key-value pairs inside of the dictionary variable. 

in the screenshots below, I have a dictionary type variable called dict1 with key value pairs key1:value1 and key2:value2. for the sake of simplicity, I have called the value1 using dict1['key1'] and printed the corresponding value with a log message while using run code snippet.

similarly, i used the log operation of the keyword test, and used the value of "code" with the corresponding dictionary syntax (i.e.:  keywordtests.dictionary1.variables.dict1['key2']) and it printed the corresponding value of the second key value pair. 

it is important to note that no matter what you do, even with a keyword test, you will need to call on the proper key value pair using the correct syntax if you are parameterizing these values into your operation.

Justin Kim

Hi, Ive tried this but it doesnt work.


How do you declare the variable , is it an Object type?

Initially, I went into the variables tab and made a new variable called dict 1, which I declared it as an empty dictionary in the default value: {}

and type: object


in hindsight, I could have just used the set variable value operation, and added in a new local variable with some name, and set it as on object, with the value {'key1':'value1', 'key2':'value2',...,'keyx':'valuex'}

then whenever you use some subsequent operation to access these key value pairs, you would use some syntax in the value parameter of that said operation like KeywordTests.myKDTest.Variables.Dict1['keyx']

Justin Kim
SmartBear Alumni (Retired)

Thanks for the help, hkim5, tristaanogre!


Do these suggestions help you? Please share your solution with use.

Tanya Yatskovskaya
SmartBear Community and Education Manager

Showing results for 
Search instead for 
Did you mean: