3
votes

The Robot Framework User Guide, section 6.6 Boolean arguments, says:

Many keywords in Robot Framework standard libraries accept arguments that are handled as Boolean values true or false. If such an argument is given as a string, it is considered false if it is either empty or case-insensitively equal to false or no. Other strings are considered true regardless their value, and other argument types are tested using same rules as in Python.

How do I replicate this behavior in my own user keywords?

The build-in keyword Convert To Boolean is stricter:

Converts the given item to Boolean true or false.

Handles strings True and False (case-insensitive) as expected, otherwise returns item's truth value using Python's bool() method.

1
are you asking h ow to do it in keywords written in python, or in robot syntax? - Bryan Oakley
I would prefer Robot syntax. - Lassi

1 Answers

3
votes

There are two functions in robot.utils for dealing with boolean arguments - is_truthy and is_falsy. DateTime uses is_falsy. To behave like that library, you could simple call the same function used by those libraries. Below is an implementation of is_falsy in Robot syntax and en example keyword using it to convert arguments. You could also convert the arguments as needed using the same evaluate statement and avoid inter-dependencies.

*** Test Cases ***
Boolean
  [Template]    Some Keyword
  truE
  ${42}
  FAlsE
  no
  ${0}

*** Keywords ***
Some Keyword
    [Arguments]    ${option}
    ${option as bool}    Is Truthy    ${option}
    Log To Console    ${option} -> ${option as bool}

Is Truthy
  [Arguments]    ${arg}
  ${arg as bool}    Evaluate    robot.utils.is_truthy($arg)    modules=robot
  [Return]    ${arg as bool}