How do I find if a variable has been defined?
Asked Answered
G

6

9

How do I find out if a variable has been defined in my Robot Framework script? I am doing API testing, not UI testing. I have a complex set up and tear-down sequence and, since I am interacting with multiple computers through the script, it is important to know the current state if a fatal error has occurred. I could track what I have done with some complex set of meta variables or a variable tracking list, but I would prefer to query if a particular variable has been defined and if so take the appropriate tear-down steps.

A simplified version is something like:

*** Test Cases ***
Check monitor
    ${monitored}=  Connect to Monitor  ${Monitor IP Address}  ${User name}  ${password}
    ${peer connected}=  Connect to Monitor  ${Peer IP Address}  ${User name}  ${password}
    Get Information from Monitor  ${IP Address}
    Send Info to Peer   ${buffer1}
    Report back to Monitor  ${Monitor IP Address}

We are assuming that the tear-down closes the connections. I want to close any connections that are open, but if I failed to open the peer connection I will close the monitor connection and fail on closing the monitor connection.

I am trying to determine if ${peer connected} is defined. Can I look into Robot Framework's variable storage to see if it is there (in that dictionary?)?

Grider answered 15/11, 2019 at 20:49 Comment(0)
O
16

You can call Get Variables to get a dictionary of all variables, then check whether the variable you're interested in is in the dictionary.

*** Test cases ***
Example
    ${foo}=        set variable  hello, world
    ${variables}=  Get variables

    Should be true      "\${foo}" in $variables
    Should not be true  "\${bar}" in $variables
Oilcup answered 15/11, 2019 at 23:10 Comment(3)
Thanks, this is exactly what I need. I can inspect for the existence of the necessary variables in this scope and Fail or proceed accordingly.Grider
What is the difference between calling the ${variables} and $variables? I'm wondering why ${variables} doesn't work, but $variables works.Unmentionable
@KonstantinV: it is described in the robot framework documentation for the built-in libraryOilcup
T
5

There a pretty straightforward approach - the built-in keyword Get Variable Value returns python's None (by default) if there is no such variable defined:

${the var}=    Get Variable Value    ${peer connected}
${is set}=      Set Variable If    """${the var}""" != 'None'    ${True}    ${False} 
Tautology answered 15/11, 2019 at 21:52 Comment(1)
In case the variable ${the var} contains the value None, which means that variable WAS defined, the condition will not be satisfied cause it contains the value NoneOcreate
O
1

I am fine with this approach. In case the variable is not defined, the test case does not fail....

${variables}  Get variables
${status}  Run Keyword And Return Status  Evaluate   $new_table in $variables
${new_table}  Set variable if  ${status}==${FALSE}  new_tbl  ${new_table}
Ocreate answered 9/9, 2021 at 11:39 Comment(0)
R
1

By this code you can check if variable exist without failing the testcase & you can create as well if you want

${var_1}  Run Keyword And Return Status  Variable Should Exist  ${var_1}
IF  '${var_1}' == 'False'
    ${var_1}  set variable  hello-world
END
Rodenhouse answered 20/11, 2023 at 20:27 Comment(0)
H
0

Also possible is:

${variables}  Get Variables
IF  "\${dataPluginVersion}" in "${variables}"
  No Operation
ELSE
  ${dataPluginVersion}  Set Variable  0
END

Or:

${variables}  Get Variables
IF  not "\${dataPluginVersion}" in "${variables}"
  ${dataPluginVersion}  Set Variable  0
END
Haver answered 18/11, 2021 at 9:14 Comment(2)
There must be variables.keys() because variables is a dict, then it works.Groundless
keys()is not the point. See this answer. The point is that "${variables}" is a text substitution and that can easily produce errors evaluating the expression (depending on the content). So, better use the Special $variable syntax. I.e. say IF "\${dataPluginVersion}" in $variables without quotes and without curly braces around variables. See also @Michal's answer.Doited
C
0

A shorter way:

OEM-T01-99-Test-variables
    [Tags]    TEST
    Variable Should Not Exist   \${TESTDEVICE_SSH_CONNECTION}
    Variable Should Exist   \${TEST_NAME}
  

This method is more readable and less verbose than using "Get Variables" keyword, IMHO

Reference: Robotframework built-in keywords

Covering answered 10/1, 2023 at 10:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.