Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the value of a dictionary element to the result of a keyword in Robot Framework?

I know that I can access a single element from a dictionary object with this format ${dict['KEY']}. Like this:

|   | Log | ${dict['KEY']} |

And I can set a regular old scalar like this:

|   | ${scalar}= | RFKeyword | "Yowp"

But if I try to set a dictionary element like this

|   | ${dict['KEY']}= | RFKeyword | "Yowp"

I get "RFKeyword", "Yowp" in the variable, rather than the result of what RFKeyword produces when processing "Yowp" like I do with this

|   | ${scalar}= | RFKeyword | "Yowp"

Assistance please

like image 908
Skip Huffman Avatar asked Jul 13 '12 17:07

Skip Huffman


2 Answers

As you probably have figured out, you can't assign to a dictionary from a keyword. You need to very specifically follow the dictionary syntax. you can only return variables to lists or scalars.

Robot framework isn't a fully fledged programming language, and it shouldn't be. By using an intermediate scalar, non-technical testers should be better able to understand what it is doing.

I added this since a google search for "robot framework dictionary" has this question high up in the list. Just create dictionaries with:

Create dictionary | ${my_dict} | a | b

Add to dictionaries with:

set to dictionary | ${my_dict} | c | d

And retrieve from dictionaries with:

${my_dict["a"]}

Or, if you need to not fail:

${my_dict.get('non-key','default value')}
like image 154
ate_f Avatar answered Sep 22 '22 19:09

ate_f


You just need to rearrange the way you call it. So for your keyword where you want the returned data to go into your dictionary you need to do the following:

${scalar}= | RFKeyword | "Yowp"
Set To Dictionary  | ${dict}  |  KEY    | ${scalar}
like image 40
Seanog Avatar answered Sep 24 '22 19:09

Seanog