0

I have a variable &{userList} and I've assigned the below value to it:

&{userList}                 id={rand_id}    extension={rand_extn}

Then I am replacing the {rand_id} and {rand_extn} in my test case as:

${uni_id}=    Generate Random String    2    [NUMBERS]
${uni_extn}=    Generate Random String    4    [NUMBERS]
: FOR    ${key}    IN    @{userList.keys()}
\    ${updated_val}=    Replace String    ${userList["${key}"]}    {rand_id}    ${uni_id}
\    Set To Dictionary    ${userList}    ${key}    ${updated_val}
\    ${updated_val}=    Replace String    ${userList["${key}"]}    {rand_extn}    ${uni_extn}
\    Set To Dictionary    ${userList}    ${key}    ${updated_val}

Now I wanted to create one variable which can have nested dictionary but I don't know how to do this. I am doing like this:

&{multipleUserList}         id={rand_id}    extension={rand_extn},  id={rand_id2}    extension={rand_extn2}

And then I wanted to replace the values like this:

${uni_id}=    Generate Random String    2    [NUMBERS]
${uni_extn}=    Generate Random String    4    [NUMBERS]
${uni_id2}=    Generate Random String    2    [NUMBERS]
${uni_extn2}=    Generate Random String    4    [NUMBERS]
: FOR    ${key}    IN    @{multipleUserList.keys()}
\    ${updated_val1}=    Replace String    ${multipleUserList["${key}"]}    {rand_id}    ${uni_id}
\    Set To Dictionary    ${multipleUserList}    ${key}    ${updated_val1}
\    ${updated_val2}=    Replace String    ${multipleUserList["${key}"]}    {rand_extn}    ${uni_extn}
\    Set To Dictionary    ${multipleUserList}    ${key}    ${updated_val2}
\    ${updated_val3}=    Replace String    ${multipleUserList["${key}"]}    {rand_id2}    ${uni_id2}
\    Set To Dictionary    ${multipleUserList}    ${key}    ${updated_val3}
\    ${updated_val4}=    Replace String    ${multipleUserList["${key}"]}    {rand_extn2}    ${uni_extn2}
\    Set To Dictionary    ${multipleUserList}    ${key}    ${updated_val4}

When I am running this and printing the values of ${multipleUserList}, I am getting values from ${uni_id2} and ${uni_extn2} but values of ${uni_id} and ${uni_extn} are not coming:

{u'id': u'60', u'extension': u'4858'}

Kindly help me in getting all the values. I am creating a body of the REST API when I've to create multiple user.

Lokesh Singh
  • 75
  • 2
  • 10

2 Answers2

0

You can do your dictionary merge directly in python : https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Evaluate

Alexis Leloup
  • 250
  • 1
  • 8
0

Dictionaries can have only unique keys normally. You can take a look at this old discussion - Since Robot is basically just a layer on top of Python, same rules apply here. You are attempting to assign key id and key extension multiple times with different values. What that does, is to update the existing keys with the new values - Instead of adding new keys.

In Robot there's a keyword in Set To Dictionary the Collections Library which can be used to add multiple key-value pairs to an existing dictionary.

Just as an example, following would achieve the situation where you have 2 unique Id keys and 2 unique extension keys within the same dictionary. This way they are not overridden during the execution.

Set to Dictionary    ${multipleUserList}    id-1=${rand_id}    extension-1=${rand_extn}    
...                  id-2=${rand_id2}    extension-2=${rand_extn2}

Notice that the dictionary is used with $ instead of &.

Also wanted to point out, your current dictionary generation system does not maintain the connection between id and extension values. Dictionaries are not ordered so if the connection of the values is important, some other organizing method should be considered.

Morkkis
  • 450
  • 3
  • 12