Is there a way to generate a random string inside a dictionary key?

Viewed 231

I have a keyword that generates multiple random data using the Generate Random String keyword and currently I'm creating variables to store this random strings generated.

The way I'm doing is like this

Generate Random Form Data
    ${variable1}         Generate Random String    8             [Letters]
    ${variable2}         Generate Random String    8             [Letters]
    ${variable3}         Generate Random String    8             [Letters]

Is there any way to generate this strings inside a dictionary, like this?

Generate Random Form Data
    ${variable}          Create Dictionary
    Set To Dictionary    ${variable}               first_name    Generate Random String    8    [Letters]

Trying to do like the example above generates a dictionary with two keys: firstname and 8, when all I wanted was creating a dictionary with a single key with random string.

1 Answers

In short - no, can't do it like this; Set To Dictionary takes whatever you pass and sets it directly as a value.
That's the same for (almost) all keywords in Robot Framework - you can't provide as an argument another keyword, the returned value of which will be used as the caller's argument.

In this case though, this is easily solvable with doing as many iterations of random value generation, and adding it to a dictionary - e.g. using a for loop:

${variable}=    Create Dictionary
FOR    ${i}    IN RANGE    10    # how many random string you need
    ${value}         Generate Random String    8
    Set To Dictionary    ${variable}     first_name_${i}    ${value}
END

You can put it in a keyword, and pass the number of key:value pairs you need.

Related