Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dictionary and pass it as a map to a Java method in Robot Framework

I need to pass a java.util.Map to a a test method In the tsv file, I tried to create a dictionary like this:

${MYDICT} =  Create Dictionary  a  1  b  2

But I got the error:

Setting variable '${MYDICT} = Create Dictionary a 1 b 2' failed: Variable name '${MYDICT} = Create Dictionary a 1 b 2' is invalid.

I declare the dictionary in the variables section like this:

    ${MYDICT}=  Create Dictionary   COUNTRY US  CURRENCY_CODE   USD

the test case is this:

    testCase1 run the test using ${MYDICT}

and the test keywords are defined like this:

    run the test using ${MAP}
        call java method ${MAP}

and the Java method is:

    public void CallJavaMethod(Map<String, String> map)

However, if I declare the dictionary in the Test keywords section, everything works fine and the Java method is called:

    run the test using
        ${MYDICT}=  Create Dictionary   COUNTRY US  CURRENCY_CODE   USD
        call java method ${MYDICT}

I do not understand why I must declare the dictionary at the point where I want to use it. What if I want to run that test case with different inputs?

Fix ( used what @Uri and @Brian suggested) I try to use the "Set Suite Variable" like in this example:

*** Settings ***
Library Collections
Suite Setup Initialize dictionary

*** Keywords ***
Initialize dictionary
    ${dict}=    Create Dictionary   COUNTRY US
    Set Suite Variable  ${dict}

*** Test cases ***
testDict
    Dictionary should contain item  ${dict} COUNTRY US

And the test passes successfully.

like image 460
Alex Avatar asked Jan 08 '23 08:01

Alex


1 Answers

I declare the dictinary in the variables section like this:

${MYDICT}=  Create Dictionary   COUNTRY US  CURRENCY_CODE   USD

That's the problem. You can't call keywords like that in the variable section. The variable table is for defining static values.

From the robot framework user's guide section on variable tables:

Their [variable table] main disadvantages are that values are always strings and they cannot be created dynamically.

If you want to create a dictionary that can be used in multiple tests, create it in a keyword and use the Set Suite Variable keyword to make it available everywhere in the suite. You can call this keyword from a Suite Setup. Or, create it in a variable file.

Example

The following example creates a suite-level variable named ${dict} which contains two keys. The dictionary is initialized in a suite setup. There are two simple tests to verify that the dictionary was set up properly and is accessible to both tests.

*** Settings ***
| Library | Collections
| Suite Setup | Initialize dictionary

*** Keywords ***
| Initialize dictionary
| | ${dict}= | Create Dictionary 
| | ... | COUNTRY       | US
| | ... | CURRENCY_CODE | USD
| | Set suite variable | ${dict}

*** Test cases ***
| Test A
| | Dictionary should contain item | ${dict} | COUNTRY | US

| Test B
| | Dictionary should contain item | ${dict} | CURRENCY_CODE | USD
like image 85
Bryan Oakley Avatar answered Feb 07 '23 19:02

Bryan Oakley