In my test I have a lot of conditional parts that are depended on the same function 'get max duration passed'
It is a python method that returns True or False
E.g.
FOR ${i} IN RANGE 9999999
${max_passed} get max duration passed
Exit For Loop If ${max_passed}
I have a lot of places where I have e.g. 'run keyword if' where I first put the outcome of 'get max duration passed' inside a variable and than use the variable.
Is there a way to use the method directly?
like:
FOR ${i} IN RANGE 9999999
Exit For Loop If get max duration passed
Using robotframework 3.1.1
The answer is no.
Because "Exit For Loop If" requires a boolean condition which can be evaluated and not a keyword or a method which returns boolean in your case.
Related
So, I have some code which is supposed to check whether a pre-check sequence has completed before I begin patching a server. I'm doing this by using an until loop and as the condition, I have a flag set to true. When the flag is set to false that means that the pre-checks are done and I can now proceed out of the until loop.
The problem is I get stuck in an infinite loop and I get an error message saying [: true: integer expression expected
I continually call a URL to conduct my checks, and it returns a file which I save. An example extract from that file would be:
inProgress":true,"status":"IN_PROGRESS","preCheckMessages":[],"precheckResultItems":[]}
The part I need to extract from this is the value from the inProgress field (which could be 'true' or 'false'). I then use this value to compare to my flag. So I extract the value from the inProgress field.
The section of code causing me an issue is as follows:
inProgress="true"
until [ "$inProgress" -eq "false" ]
do
long_URL_goes_here > jobIdCheck.txt
inProgress=$(grep -o 'inProgress.*$' jobIdCheck.txt)
word="inProgress\":"
tempVar=${inProgress##${word}}
inProgress=${tempVar%%,*}
echo inProgress value = ${inProgress}
done
echo "Pre-checks complete. Ready to apply patch"
This is where I hit the infinite loop with the error message saying [: true: integer expression expected. (I never see the "Pre-checks complete message")
But I don't understand why it would be expecting an integer? Surely the string comparison should suffice?
Is the fact that the value true in the inProgress field ISN'T encapsulated in double quotes important?
Any help will be greatly received.
${rowcount}= Keyword1 Book1.xlsx 0
${length}= Set Variable ${rowcount}
${i} Set Variable 1
:FOR ${rowvalue} IN RANGE ${rowcount}
\ #{columnlist}= Keyword2 ${rowvalue}
Keyword2 is returning List of data. I want to check whether it is returning Empty List. Please help me with this?
The BuiltIn library has keywords Should be empty and Should not be empty which can be used to validate the length of a list.
Should Be Empty ${columnlist}
Should Not Be Empty ${columnlist}
Just in case someone else comes here looking for an answer which also addresses:
If ${columnlist} is not empty then I have to execute keyword below it. Is it possible using If statement? – Orsu Suni Jun 12 '17 at 4:44
Either one of these options should help (NOTE: only used within RIDE, I imagine they will work for others as well):
${len} Get Length ${columnlist} ---- will return zero if list is empty, you can then use ${len} in your conditional.
'#{columnlist}' == '#{EMPTY}' ---- should return true if list is empty, although so far I have only used it with RUN KEYWORD IF.
Alternatively you could run the following keyword:
${isEmpty} Run Keyword And Return Status Should Be Empty ${columnlist}
Then you get a boolean value in ${isEmpty} with whether the list is empty or not.
Statements/conditions in Robot are a bit confusing in my opinion. Previous suggestion 2. doesn't work for me. I'm using: Robot Framework 3.1.2 (Python 3.4.1 on win32)
I obtain expected solution:
*** Test Cases ***
TC1
${marker_files} Create List dummy3 dummy4 dummy5
Run keyword unless ${marker_files} == #{EMPTY} Operation on list
marker_files=${marker_files}
tc2
${marker_files} Create List #{EMPTY}
Run keyword unless ${marker_files} == #{EMPTY} Operation on list
marker_files=${marker_files}
*** Keywords ***
Operation on list
[Arguments] ${marker_files}=def
log to console \ndo something on list
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.
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}
Robot is telling me that I'm providing too many arguments to my keyword. I've boiled it down to a base case where I have a keyword that should do nothing:
def do_nothing():
"""
Does absolutly nothing
"""
Calling this keywork like this:
*** Test Cases ***
testCaseOne
do_nothing
Give this result:
TypeError: do_nothing() takes no arguments (1 given)
Adding a parameter to the keyword definition fixes the problem. Why does robot seem to pass 1 parameter to each keyword, even if there are no parameters in the test case?
I found the answer here.
The issue has nothing to do with the robotframework, and has every thing to do with Python; Python implicitly passes the current instance of the class to method calls, but I needed to explicitly declare the parameter. This is customarily named self:
def do_nothing(self):
This test runs.
I am trying to assign a variable - body ,depending on the status of another variable phonenumber_id. If the phonenumber_id is NULL, body gets assigned False.
But it doesnt seem to be working. It works only if he phonenumber_id is not NULL.
${body}= Run keyword if '${phonenumber_id}'!='NULL' Set variable TRUE
... Else Set Variable FALSE
Not sure what i am doing wrong.
The keyword Set Variable If will set a variable based on a given condition
${body}= Set Variable If '${phonenumer_id} != 'NULL'
... ${True} ${False}
You got it almost right - just mistyped the ELSE - it must be in capital letters, to be considered a part of the Run Keyword If. So in your particular case, it should've been:
${body}= Run keyword if '${phonenumber_id}'!='NULL' Set variable TRUE
... ELSE Set Variable FALSE
For the simple case of just setting a new constant value though, #ILostMySpoon answer is good enough - and more "readable".
In general, for someone stumbling on this post, the Run Keyword If combined with ELSE Set Variable is a very powerful construct to set/change a variable - based on the fact that it not only runs a keyword(s) conditionally, but also propagates its return values back to the stack.
Consider this example:
${var}= Run Keyword If ${bool condition} Do Some Action Returning A Value
... ELSE Set Variable ${var}
In it {var} will be set to the return value of Do Some Action Returning A Value only if ${bool condition} evaluates to true, and will keep its old value otherwise.
Another artifical but less abstract example:
${value}= Run Keyword If ${should be int} Convert To Integer ${value}
... ELSE IF ${should be float} Convert To Number ${value}
... ELSE Set Variable ${value}