3

I am new to robot framework, and inherited some .robot files which call some python functions.

Within the .robot file we have some variables defined as:

| *** Variables ***  |
| ${file}            | 2021
| ${useV2}           | False

However when the python function is called and this code runs:

log.info(f"{useV2} , type: {type(useV2)}")

the output is

False , type: <class 'str'>

So short of checking for "False" and "True" in this function, and subsequent functions where needed, how do I setup a robot framework variable as a boolean and have python derive true/false accurately?

1
  • The above case looks as expected, what are you actually looking for? Commented Apr 15, 2021 at 6:11

2 Answers 2

4

Two options - either use the syntax to declare it as the actual boolean type:

${useV2}           ${False} 

(this works for declaring ints or floats too), or use the keyword Convert To Boolean, that handles the strings "True" and "False":

${useV2}=    Convert To Boolean    ${useV2}

The section in the user guide talking about this variable syntax - http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#boolean-and-none-null-variables ; the integers/floats are described in the section above it.


And here's a sample; with this RobotFramework code:

*** Variables ***
${booly}      ${True}
${stringy}    True

*** Test Cases ***
A test
    Do It       ${booly}
    Do It       ${stringy}
    ${cast to booly}=       Convert To Boolean      ${stringy}
    Do It       ${cast to booly}

, and this python function:

from robot.libraries.BuiltIn import BuiltIn

def do_it(useV2):
    BuiltIn().log_to_console(f"{useV2} , type: {type(useV2)}")

, the output in the console is:

True , type: <class 'bool'>
True , type: <class 'str'>
True , type: <class 'bool'>

E.g. the variable defined as ${True} is <class 'bool'>, the one defined as True is <class 'str'>, and the later after going through Convert To Boolean becomes bool itself.

Sign up to request clarification or add additional context in comments.

4 Comments

I felt like the op's issue is that when python keyword is called, the argument passed into it is of type str, not bool as intended .. So while your examples are correct, they might not be related to op's question - at least fully..
On the contrary, he has provided the perfect example - he is creating a variable in RF, and calling type() in python, to check its type. As I've commented in your answer, the framework will pass the value as is - and in his case, a string. And it's a string, because by default this is what you get when you define a variable; if you want int, you'd do ${123}, if float - ${123.45}; but with just 123 you will get a string that looks like the number when printed, but is in fact - a string.
@TodorMinakov this does not work. In RF I have $var = ${False} and then in python I use tyoe() and it still says str
@andrewb I very much doubt it, just updated the answer with a sample showing it actually works as described.
0

Older RF versions always passed parameters to python land as strings. So, python code was supposed to always do a typecast to a proper type. Here's a issue about the topic: https://github.com/robotframework/robotframework/issues/2890

Newer versions when that landed do a type conversion if your python code has type annotation for the parameter (with few caveats).

So, I'd assume that your python function that is a keyword in RF, does not have type annotation ..

6 Comments

That's not true - in RF the variable is passed as is, the same type it has been created for; the only exception is keyword with embedded arguments, because of their nature - the argument is inside a string, thus it becomes a string. So if your variable is a list, tuple, object - this is what the called keyword or method will receive; otherwise 3/4 of the framework would simply not work. In the newer versions, the framework does support type annotations in python methods - and with those, it tries to cast the value, does not guarantee 100% success.
semantics .. In any case, you have to quarantee that the type you get into python side will be off correct type yourself exactly because what i said. Variable data can come from anywhere, not just what you type into your resource files with or without correct syntax or explicit type casting with other provided kw's. Type annotations do help and would probably be "easiest" solution in this case.
And yeah, you are correct, they are passed as is -- but in the Q, op is using literal strings. You correctly pointed out that using those two ways would fix this issue. However, as the OP did point out the python code is theirs, wouldn't it make more sense to guide him to fix the issue in the python and learn how things work versus not even mentioning how things really work ?
This is hardly semantics, @rasjani, this is how the framework, and the underlying language work. Python is dynamically typed language, in any method you define the parameters, add docstrings, in the recent versions add annotations; but - the caller can pass any type in the arguments, regardless of the docstring, or the annotations; And Bad Things Can Happen ™. Yet this is how the language works; and robotframework, built on top of it works the same way. You can take precautions - by checking the type, casting, throwing exceptions, to name a few. Now to the OP's question:
The title of the question is literally - "How to pass a boolean variable to a python function from robot framework" - "how to create a boolean and pass it as this type". In the body he explicitly states the same - "... how do I setup a robot framework variable as a boolean and ...". Now, if the question was "I have this python code I can mutate in any way to dance around it, what would be the preferable way to do that", then we could be talking about annotations, checks and casting. But I hardly believe this is the case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.