Forum Discussion

gjmaster018's avatar
gjmaster018
New Contributor
5 years ago

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 

{key1:value1,key2:value2}

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?

  • 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.

  • tristaanogre's avatar
    tristaanogre
    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.

  • 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.

    • gjmaster018's avatar
      gjmaster018
      New Contributor

      Hi, Ive tried this but it doesnt work.

       

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

      • hkim5's avatar
        hkim5
        Staff

        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']