How to pass arguments to keywords from testcase using robotframework? - robotframework

i want to pass the arguments from testcase to keyword.
what i am trying to do?
i have the testcase with arguments like below
*** Test Cases ***
Test something happens
Login
${val2} = somevalue1
${val2} = somevalue2
${name} = somename
Draw something ${name} ${val1} ${val2}
*******keywords************
Draw something
Input Text ${name_input} ${name}
Input Text ${name_input} ${val1}
Input Text ${name_input} ${val2}
How can i pass the arguments from testcase to keyword Draw something
i was trying to pass it directly to keyword like below
*********keywords*******
Draw something ${name} ${val1} ${val2}
but gives error keyword expected 0 arguments but got 3
could someone help me with this. thanks.

Here is the documentation on how to use arguments with Robot Framework keywords:
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#using-arguments
And here is an example printing out a full name based on arguments first and last:
*** Keywords ***
Print Name
[Arguments] ${FIRST} ${LAST}
Log To Console ${FIRST} ${LAST}
*** Test Cases ***
Test printing a name
Print Name John Doe
As you can see, you need to add the [Arguments] section under your keyword:
*** Keywords ***
Draw something
[Arguments] ${name} ${val1} ${val2}
Input Text ${name_input} ${name}
Input Text ${name_input} ${val1}
Input Text ${name_input} ${val2}

Related

How to identify if a value is integer or number?

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')

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.

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

How can we use "Set Variable if" with a keyword where Keyword is returning a variable RobotFramework

How can we use Set Variable if with a keyword, where Keyword is returning a variable that needs to be Set in RobotFramework.
Eg: ${Var} = set variable if ${i}==10 Keyword.
Actually, the easiest way to do it is by using Run Keyword If instead of Set Variable If like below:
Foo
${ret}= Run Keyword If ${i} == 10 Keyword Which Return Something
Should Be Equal ${ret} something
Keyword Which Return Something
${var}= Set Variable something
[Return] ${var}
One way to do this would be the usage of "Run keyword if" with "set test variable" eg.
*** Test cases ***
foo
Run keyword if ${i} == 10 kw that sets test variables
should be equal ${var} HELLO
*** keywords ***
kw that sets test variables
set test variable ${var} HELLO
The way I would do it is:
*** Test Cases ***
Test Case Title
${passed} = run keyword and return status
... Should be equal ${i} 10
${var} = set variable if ${passed} It is today
Another Test Example
${var} = set variable if ${i}==10 It is today
*** Keywords ***
It is today
${today} = Get Current Date UTC result_format=%-d-%-m-%Y exclude_millis=true
[Return] ${today}
Read further in the documentation
here and here.
Just store the value from Keyword in a temp variable:
${temp} = | Keyword | Param1 | Param2 | .....
${Var} = | ${i} == 10 | ${temp}
If i is 10, ${Var} will be set to the return of Keyword.

Resources