How to identify if a value is integer or number? - robotframework

I want to identify if a value stored in a variable is an integer or not. If its an integer then it should return a Boolean value
I have tried using Built-In functions like Should be Equal As Numbers, Should be Equal As Integers but they did not work.
Since I am not so sound in Python, hence I was not able to make use of the Python built-in functions but I have a strong feeling that python functions like .isdigit() or .isnumeric() can come in handy here.
I am storing some value in a variable, say ${TestVariable}
Now, I have tried identifying the stored value as integer via following ways:
${Status} Run Keyword and Return Status Should be Equal As Numbers ${TestVariable} 1
Log to Console \n ${TestVariable}-${Status}
And I have passed values like
a,b,1,2
Since I have hard coded value 1 in Should Be Equal As Numbers, hence it returned True when the value stored in ${TestVariable} is 1 but returned False when the value was 2
Actual Result:
a-False
b-False
1-True
2-False
Expected Result:
I want Robot to return True when value is a number and False when its a character like below
a-False
b-False
1-True
2-True

Here is a possible solution, please note that "2e10" is converted to number, but the keyword does not consider that.
*** Test Cases***
Verify Types
FOR ${item} IN two ${None} 1235 2.567 2e10
${result}= Check Type ${item}
Log Item ${item} is ${result}
END
*** Keywords ***
Check Type
[Arguments] ${object}
[Documentation] Checks if the ${object } is INTEGER, NUMBER or STRING
Return From Keyword If not "${object}" NONE
${result} ${value}= Run Keyword And Ignore Error Convert To Number ${object}
${isnumber}= Run Keyword And Return Status Should Be Equal As Strings ${object} ${value}
${result} ${value}= Run Keyword And Ignore Error Convert To Integer ${object}
${isinteger}= Run Keyword And Return Status Should Be Equal As Strings ${object} ${value}
Return From Keyword If ${isnumber} NUMBER
Return From Keyword If ${isinteger} INTEGER
Return From Keyword STRING

if type(a) == int:
print('the value is integer')
a = "sabuj"
if type(a) == str:
print('the value is string')
a = [1,2,3]
if type(a) == list:
print('the value is List')

Related

How to get the changed content (string) after first loop iteration to be used in next iteration in Robot framework

I have a list of strings where a specified part should be replaced with another.
String_01:
UNH+000000'
BGM+ZLI+000000'
DTM+3:20210430:102'
RFF+AFL:000000'
RFF+CO:186577666'
RFF+PK:000000'
MOA+125:000000'
MOA+150:000000'
UNT+26+000000'
String_02:
UNH+000000'
BGM+ZLI+000000'
DTM+3:20210430:102'
RFF+AFL:000000'
RFF+CO:186578112'
RFF+PK:000000'
MOA+125:000000'
MOA+150:000000'
UNT+26+000000'
String_03:
UNH+000000'
BGM+ZLI+000000'
DTM+3:20210430:102'
RFF+AFL:000000'
RFF+CO:186601036'
RFF+PK:000000'
MOA+125:000000'
MOA+150:000000'
UNT+66+000000'
Then I have a dictionary key containing the things what should be replaced and value has the new value.
&{PO_PAIRS} 186577666=4500000842 186578112=4500000843 186601036=4500000844
Now I iterate through every string with the dictionary. If the dictionary key is found from the string, it's changed to dictionary value.
NewString
[Arguments] ${string}
FOR ${key} IN #{PO_PAIRS}
${value} = Get from dictionary ${PO_PAIRS} ${key}
${changed_string} = Replace string ${string} ${key} ${value}
END
[Return] ${changed_string}
The problem is that after first iteration of the loop, the ${changed_string} should be the starting point for the next iteration instead of ${string}. Is there some way to set the ${changed_string} for the next iterations in the loop?
problem is that after first iteration of the loop, the ${changed_string} should be the starting point for the next iteration instead of ${string}
Yep, in every iteration you're redefining ${changed_string}, and thus at the end it's ${string} with just the last change.
Don't use an intermediate variable, reassign the replaced string to ${string}, and return it:
NewString
[Arguments] ${string}
FOR ${key} IN #{PO_PAIRS}
${value} = Get from dictionary ${PO_PAIRS} ${key}
${string} = Replace string ${string} ${key} ${value}
END
[Return] ${string}

How to set default value for an optional list parameter in keyword

I've previously used a list as parameter to take in a variable/optional number of arguments to a keyword, and this has worked perfectly:
Keyword Name
[Arguments] ${otherVariable} #{args}
....
My question is how do I set up a default value for this, if the user omits any more values?
i.e. something like
Keyword Name
[Arguments] ${otherVariable} #{args}=['0']
....
Check is ${args} empty, and if so - set the default value to it:
Keyword Name
[Arguments] ${otherVariable} #{args}
${args}= Run Keyword If not $args Create List 0
... ELSE Set Variable ${args} # varags were passed, leave it as is
This is analogous to this python code (RF is based on it, so a lot of approaches / recipes are the same/pretty close):
def keword(otherVariable, *args):
if not args: args = [0]

How to use both Run Keyword If and Run Keyword and Return Status?

file.robot
Keyword1
log this is keyword1
${some_value} = Set Variable Hello, world!
[Return] ${some_value}
file2.robot
Some_name
Run keyword If 'True' == 'True Run Keyword and return Status Keyword1
I want to use this way. How do i access the return value in file2.robot
Above, 'Some_name' in file2.robot calls the 'Keyword1', the return value 'some_value' to be printed in 'Some_name' of file2.robot.
How can it be achieved in one-liner as stated above ?
You cannot use a "Run keyword..." command and both get a return value and a pass/fail value. However, if all you need is the return value, Run keyword if will return the result of the keyword that it runs.
For example:
*** Test Cases ***
Example
${the_value}= run keyword if 'True' == 'True' keyword 1
With the above, keyword 1 will only run if the expression evaluates to true. ${the_value} will be set to the result of keyword 1.
If you need both the status and the returned value, you can use Run keyword and return status to run the keyword, but you'll have to modify the keyword to set a suite or global variable that your test can get after the keyword returns.

Compare FALSE expression in Robot Framework Test Cases

I'm having issue in negate the bool variable or comparison of FALSE against a bool variable to perform Run Keyword If
My Code is
Test Case
${isExist}= Run Keyword And Return Status Element Should Be Visible ${element_Id}
Run Keyword If ${isExist} == false click element ${cancelbtn}
Here I'm facing an Run time error
Evaluating expression 'True and' failed: SyntaxError: unexpected EOF
while parsing (, line 1)
I tried the following comparison too
${isExist} == 'false'
'${isExist}' == 'false'
${isExist} == ${false}
'${isExist}' == '${false}'
!${isExist}
Note: log to console ${isExist} - It logs the appropriate Boolean value in the console window.
if ${isExist} is a boolean, you can use not
Run Keyword If not ${isExist} ...
You can also compare to ${FALSE} or ${TRUE}:
Run Keyword If ${isExist} is ${FALSE} ...
Run Keyword If ${isExist} is not ${TRUE} ...
You say that ${isExist} == ${false} doesn't work, but it will if {isExist} is indeed the boolean value False.
Note: ${FALSE} and ${TRUE} are variables defined by robot. They are briefly mentioned in the documentation in the section titled Boolean and None/null variables in the robot framework user guide.

Run keyword if to append to a string ... ${query_string}= catenate ${query_string} AND

I want to append to a variable if dictionary size is greater than 1
${queryString}= startOfString
Run keyword if ${dictionary_size} > 1
... ${query_string}= catenate ${query_string} restofString
However the only if statement i can see in Robot is the above. Obviously variable assignment isnt a keyword. Is there another way of doing this so i would end up with
startOfString restofString
Set Variable If is your friend here.
${queryString}= Set Variable startOfString
&{dict}= Create Dictionary foo=bar
${dictLen}= Get Length ${dict}
${queryString}= Set Variable If ${dictLen} > 1 ${queryString} restofString ${queryString}
If the start of the query is static:
&{dict}= Create Dictionary foo=bar zaz=lop
${dictLen}= Get Length ${dict}
${queryString}= Set Variable If ${dictLen} > 1 startOfString restofString startOfString

Resources