Robot Framework: How to know that variable is list, dictionary or regular

Question:

I need to process several types of data from one Keyword – which will call another necessary Keyword according to the defined data type.

Using Evaluate type() is good for Lists and Dictionaries but fails when we have just a string without quotes. If I add quotes – the data will be defined as string all times – even if it is in a List or Dictionary.

For example

${list}=    Create List    1    two
${type_list}=    Evaluate    type(${list})
# returns <type 'list'>

${dict}=    Create Dictionary  first=1  second=two
${type_dict}=  Evaluate  type(${dict})
# returns <type 'dict'>

${string}=  Set Variable  withoutQuotes
${type_string}=  Evaluate  type(${string})
# FAILS with:
Evaluating expression 'type(withoutQuotes)' failed: NameError: name 'withoutQuotes' is not defined

Could you please recommend some way to define a type of a variable which won’t fail?

Asked By: bp3

||

Answers:

Run Keyword And Return Status has helped me!

${passed}=    Run Keyword And Return Status   Evaluate    type(${value})
${type}=      Run Keyword If     ${passed}    Evaluate    type(${value})
Answered By: bp3

In the new versions of Robot Framework you can call the object passing the variable without the braces, like this:

Test Type
    ${list}=    Create List
    ${type_list}    Evaluate    type($list)
    log    ${type_list}    warn
    # returns: <class 'list'>

    ${string}=  Set Variable  withoutQuotes
    ${type_string}=  Evaluate  type($string)
    log    ${type_string}    warn
    # returns: <class 'str'>
Answered By: Samuel Cabral
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.