Monday, 22 June 2015

Robot Framework - How to update a value in a Python Robot dictionary

So, {} represents a dictionary,
e.g.
{'key': value}

and we can get a value out of a Robot Framework dictionary via 
${dictionary}['key']

One would assume you could then go 
${dictionary}['key']=    Set Variable    Hello World

but no, this will not set the value; we are not referencing an object in a dictionary, but trying to set a variable of a string, which confuses Robot. 

What existing keywords exist for this? 
Well I was surprised to find no built in RFW keyword for setting. There is after all a 
Get From Dictionary    ${dictionary}    ${key}
[Return]    ${value}

After messing around, I realised it would take a fraction of the time to create a Python keyword. And this brings me to my recommended solution. 

    def update_in_dictionary(selfself, dictionary, key, newValue):
        """
        Updates a value in a dictionary. Returns the dctionary
        """
        dictionary[key]=newValue
        return dictionary

QED.

1 comment:

Robot Framework, Basic Setup

Plug: Robot Framework is quick to setup, easy to write tests for, and super fast to triage failures in. The last point really sets it apart...