Multiple if/else condition in robotframework - robotframework

I'm a newbie. I'm using robot framework to implement my test cases. There is a business logic need to check multiple conditions in IF clause like
Run Keyword If <condition1>, <condition2>,...
ELSE do something
I tried this but It's not working
Single condition -> Work
Run Keyword If 'a' == 'a' log a
... ELSE Log b
Multiple conditions -> Not work
Run Keyword IF 'a' == 'a' and 'b' == 'b' log a
... ELSE Log b

The entire condition needs to be given to the keyword as a single argument, meaning you can't use more than one consecutive space in the expression.
Run keyword if 'a' == 'a' and 'b' == 'b'
... log a
... ELSE log b

Related

What is the Most Rly Way for Lazy Conditional Evaluation

The case. I have a part of code like this: if (exists("mybooleanvar") & mybooleanvar) {statement1} else {statement2}. I expect that if the conditions are lazily (short-circuit) evaluated astatement1 will be run if mybooleanvar is not assigned and statement2 will be called if mybooleanvar does not exist or equals FALSE.
But in practice I am getting a runtime error showing that the value of mybooleanvar is acessed and compared to TRUE if exists("mybooleanvar") == FALSE. So the complete boolean evaluation takes place.
Of course the issue can be solved by enclosed if statements with outer ones evaluating exists() and inner ones - booleans. But I wonder what is the most Rly way to properly avoid evaluation of n'th members of conditional statement if the result becomes known despite the values of further statements.
For example statement1 & statement2 will be FALSE if statement1 == FALSE. statement1 | statement2 is TRUE if statement1 == TRUE and statement2 needs not to be checked (or at least this check can be switched off by something like compiler directive {$B-) in Delphi).
Here I would use && instead of &. They differ in two ways (cf. ?"&&"):
The shorter form performs elementwise comparisons ...
and:
The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.
Example:
foo <- function()
if (exists("x") && (x)) cat("is TRUE\n") else cat("not existing or FALSE\n")
x <- TRUE
foo()
x <- FALSE
foo()
rm(x)
foo()
More can be found in this post.

Error "a name is not defined" when using a variable in expression

I am trying to test against multiple environments with a single test case using the passing in of a variable from the command line. Using the following command line:
robot --variable TESTENV:prod advertisingdisclosure_Page.robot
I need to test the value of TESTENV and depending on the value passed in set a different variable, specifically a URL, to the appropriate value. With the following code in the first keyword section of the test case I get an error:
IF ${TESTENV} == "uat"
$(MAIN_URL)= Set Variable ${env_data['uat_url']}
ELSE IF ${TESTENV} == "dev"
${MAIN_URL}= Set Variable ${env_data['dev_url']}
ELSE IF ${TESTENV} == "prod"
${MAIN_URL}= Set Variable ${env_data["prod_url"]}
ELSE
Fail "No URL specified"
END
I get the following error:
Evaluating expression 'prod' failed: NameError: name 'prod' is not defined nor importable as module
The examples I have found show how to use the Global Variable directly, but not how to evaluate it for a specific value.
Help.
Jeff
You need to put quotes around the variable name - otherwise the framework just puts its value in, and passes that expression to python.
Thus it becomes prod == '"uat", and py rightfully complains there is no defined variable prod.
The fix is simple - just surround the usage in quotes, and after substitution this will become a string comparison:
"${TESTENV}" == "uat"
Alternatively you can use another syntax - no curly brackets, which tells RF to use/pass the variable itself, not its value, and then it will be a defined one for python:
$TESTENV == "uat"

How to null check List in Robot FrameWork

${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

How to Assign a variable after running Run keyword If - else ?

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}

Job condition with global variable

I need to schedule a job. I want it to run if, and only if some global variable (let's say RUNNING_ID) is not equal to 0.
Is that possible ?
If it is, waht is the Condition syntax for the job description ?
I tried RUNNING_ID!=0, and got this error :
CAUAJM_E_10037 CONDITION ERROR: Illegal Key Word: RUNNING_ID!=0
CAUAJM_E_10281 ERROR for Job: CVAT-TENSEN2CUX-launch_decision < Error in Starting Conditions. >
Ok, figured it out :
The syntax :
VALUE(global_variable_name) operator value
with operator being one of these : =, !=, <, >, <= and >=

Resources