Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find if a variable has been defined?

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?)?

like image 244
DDay Avatar asked Dec 06 '22 09:12

DDay


2 Answers

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
like image 177
Bryan Oakley Avatar answered Dec 28 '22 08:12

Bryan Oakley


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} 
like image 30
Todor Minakov Avatar answered Dec 28 '22 07:12

Todor Minakov