I have the for loop below and I am trying to place a condition for it:
: FOR ${i} IN RANGE ${size}
\ Validate Item List ${items[${i}]}
so that the for runs only if the condition is met.
I tried "Run keyword if", but that does not seem to work:
Run keyword if ${flag}>0 : FOR ${i} IN RANGE ${size}
... \ Validate Item List ${items[${i}]}
I get "No keyword with name ': FOR' found.".
Note: flag can be zero or negative.
'Run Keyword If' keyword cannot be used directly for "For-Loop".
For-loop statements should be mentioned inside an user defined keyword and then 'Run Keyword If' should be mentioned as below:
User Defined function for For Loop
: FOR ${i} IN RANGE ${size}
\ Validate Item List ${items[${i}]}
Run Keyword If ${flag}>0 User Defined function for For Loop
From code perspective I can imagine two ways of doing the same:
${size} = Run Keyword If "${flag}">"0" Get Size #Whatever you want here
... ELSE Set Variable 0 #0 in order to make the loop not looping at all
: FOR ${i} IN RANGE ${size}
\ Validate Item List ${items[${i}]}
Or alternatively you can do following:
: FOR ${i} IN RANGE ${size}
\ Exit For Loop If "${flag}">"0"
\ Validate Item List ${items[${i}]}
Related
I need to start my variable ${i} containing int 1, to utilize the variable in xpath.
FOR ${i} IN RANGE 1 ${row}
${SKU} Get Text xpath://*[#id="tblList"]/tbody/tr[${i}]/td[1]
Input Text ${Devolucoes.addEtiqueta} 000000${SKU}0000
Click Element ${botoes.btnAdicionar}
END
You can assign int values to variables by putting the int inside the brackets like:
${i} Set Variable ${1}
or
${i} Set Variable ${10}
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')
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]
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.
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