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

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}

Related

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"

Robot FW : Buitlin library : Set Suite Variable : How to pass the variable within the current test scope to a new test scope?

Acc.to the documentation:
(1) If a variable already exists within the current scope, the value can be left empty and the variable within the new scope gets the value within the current scope.
The goal is to implement an example to statement (1).
Here is an attempt:
(Test 1/2) Use "Set Suite Variable" : Set Locally Scoped Variable As Suite Scoped
[Documentation] If a variable already exists within the current scope
... (i.e. Test 1/2), the value can be left empty and
... the variable within the new scope (i.e. Test 2/2) gets
... the value within the current scope (i.e Test 1/2)
${local_to_suite_scoped} = Set Test Variable ${3}
# intentionally not setting any value to comply with the statement (1)
Set Suite Variable ${local_to_suite_scoped}
(Test 2/2) Use "Set Suite Variable" : Use local_to_suite_scoped in this test
Variable Should Exist ${local_to_suite_scoped}
Should Be Equal ${local_to_suite_scoped} ${3} # fails with None != 3, expected 3 == 3
Test 2/2 fails but why? Is statement (1) incorrect or are the test cases implemented incorrectly? If the test cases are implemented incorrectly, can you provide the correct implementation?
Set Test Variable (like Set Suite Variable -keyword) expects that the first argument is the name of the variable. Second variable will then be the value of the variable. So instead of
${local_to_suite_scoped} = Set Test Variable ${3}
do
Set Test Variable ${local_to_suite_scoped} ${3}
See more at http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Suite%20Variable

UNIX - Until loop condition expects integer from a string?

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.

KEYWORD_SET in IDL

I am new to IDL and find the KEYWORD_SET difficult to grasp. I understand that it is a go no go switch. I think its the knocking on and off part that I am having difficulty with. I have written a small program to master this as such
Pro get_this_done, keyword1 = keyword1
WW=[3,6,8]
PRINT,'WW'
print,WW
y= WW*3
IF KEYWORD_Set(keyword1) Then BEGIN
print,'y'
print,y
ENDIF
Return
END
WW prints but print, y is restricted by the keyword. How do I knock off the keyword to allow y to print.
Silly little question, but if somebody can indulge me, it would be great.
After compiling the routine, type something like
get_this_done,KEYWORD1=1b
where the b after the one sets the numeric value to a BYTE type integer (also equivalent to TRUE). That should cause the y-variable to be printed to the screen.
The KEYWORD_SET function will return a TRUE for lots of different types of inputs that are basically either defined or not zero. The IF loop executes when the argument is TRUE.
Keywords are simply passed as arguments to the function:
get_this_done, KEYWORD1='whatever'
or also
get_this_done, /KEYWORD1
which will give KEYWORD1 the INT value of 1 inside the function. Inside the function KEYWORD_SET will return 1 (TRUE) when the keyword was passed any kind of value - no matter whether it makes sense or not.
Thus as a side note to the question: It often is advisable to NOT use KEYWORD_SET, but instead resort to a type query:
IF SIZE(variable, /TNAME) EQ 'UNDEFINED' THEN $
variable = 'default value'
It has the advantage that you can actually check for the correct type of the keyword and handle unexpected or even different variable types:
IF SIZE(variable, /TNAME) NE 'LONG' THEN BEGIN
IF SIZE(variable, /TNAME) EQ 'STRING' THEN $
PRINT, "We need a number here... sure that the cast to LONG works?"
variable = LONG(variable)
ENDIF

Set Variable to Empty Dict if None in Robot Framework

I'm looking to recreate this in a test suite:
if value is None:
value = {}
I plan to be adding to the dictionary so the following does not work:
${default}= Create_Dictionary
${value2}= Set_Variable_If ${value} is ${None} ${default}
... ${value}
When I add a key to ${value2} it gets added to ${default} (most likely due to how Python passes around references).
If you are using robot framework 2.9 or greater you can use variables directly in python expressions by omitting the curly braces. If you have a robot variable named ${value}, you can reference it as $value where robot is expecting a python expression (such as with the evaluate keyword).
For example:
${value2}= evaluate {} if $value is None else $value
You can use Run Keyword If to run a keyword to set a variable. This can call Create Dictionary and create a new & unreferenced dictionary to set your variable.
${value}= Run Keyword If ${value} is ${None} Create Dictionary
... ELSE Set Variable ${value}

Resources