How to make Robot Custom Library name as a keyword argument? - robotframework

I want to use given library name as an argument in my keyword as in the following:
*** Settings ***
Library some_library WITH NAME some_lib_name
*** Keywords ***
FOO
[Arguments] ${some_lib_name} ${params}
${some_lib_name}.SEND param1
But the robot framework gives an error "No keyword with name '${some_lib_name}.SEND' found."
So is there a way to make it work?

You can use run keyword since it will evaluate variables before calling the keyword:
*** Keywords ***
FOO
[Arguments] ${some_lib_name} ${param1}
Run keyword ${some_lib_name}.SEND ${param1}

Related

How to assign robot framework global variable to another variable in another robotfile

I have assigned global variable as ${googlesite} = http://google.com and I want to use this variable in another robot file under variable section as
robot1.robot
${googlesite} = http://google.com
*** keywords ***
set suite variable ${googlesite}
robot2.robot
*** Variables ***
${googlelogin} = ${googlesite}/login.html
*** Keywords ***
log to console ${googlesite}-- Its printing as http://google.com
log to console ${googlelogin}-- printing only ./login.html
(not appending ${googlesite} from variables section)
You can do this by executing the variable assignment using a keyword.
So in robot1.robot, define a keyword to set this variable:
*** Keywords ***
Setup
Set Global Variable ${googlesite} http://google.com
Then import robot1.robot in robot2.robot as a resource and execute the Setup keyword anywhere before accessing the variable:
*** Settings ***
Resource robot1.robot
Suite Setup Setup
*** Test Cases ***
Test global variable
# The global variable is available in robot2.robot
log to console ${googlesite}
# Use it to form the new variable
${googlelogin} = Convert To String ${googlesite}/login.html
log to console ${googlelogin}
This will print:
Test global variable http://google.com
http://google.com/login.html
| PASS |
I can't see how the code you've provided is working at all given you're using log to consoles directly in keywords section and all the spacing is wrong.
You can import robot1.robot into robot2.robot and robot2.robot will have access without the need to set it as a suite variable.
robot1.robot:
*** Variables ***
${googlesite} http://google.com
robot2.robot:
*** Settings ***
Resource robot1.robot
*** Variables ***
${googlelogin} ${googlesite}/login.html
*** Test Cases ***
Check the Google variables are correct
log to console \nGoogle Site: ${googlesite}
log to console Google Login: ${googlelogin}

how to assert key in robot framework

I have trouble with assert keys in my code.
No keyword with name 'in {'name': '$.data[0].name'}' found.
my code is
*** Variables ***
&{name} name=$.data[0].name
*** Variables ***
Run keyword if 'name' in ${name} log name is in the log as expected
The way you've defined your test cases (assuming the second *** Variables *** is actually *** Test Cases ***), robot thinks Run keyword if is the name of a test case, and the first keyword is 'name' in ${name}
The solution is to give your test case a name.
There's also a problem that you have two spaces after 'name' which should give you a different error than what you're reporting. Also, the second table should be named *** Test Cases ***.
*** Test Cases ***
Example test case
Run keyword if 'name' in ${name} log name is in the log as expected
You should probably also use $name rather than ${name} so that robot injects the actual variable into the expression.
*** Test Cases ***
Example test case
Run keyword if 'name' in $name log name is in the log as expected

Use Keywords in variable section- Robot framework

i am trying to create a variable in the variables section but by appreciating it i am doing something wrong.
so I have it now and it works.
*** Variables ***
*** Test Cases ***
MyTest
${DATA}= read_csv_file ../data.csv
Log ${DATA}
read_csv_file is a keyword that i design.
I would like it to work like this.
but the log only shows "read_csv_file ../data.csv"
*** Variables ***
${DATA}= read_csv_file ../data.csv
*** Test Cases ***
MyTest
Log ${DATA}
The Variables section does not allow to execute keywords, only to define variables, eventually using other variables.
To do what you want, you need to add a Variables python file import, where you can pass an argument to it. For example:
*** Settings ***
Library SomeLibrary.py
Variables variables_from_csv_file.py ../data.csv
Your variables_from_csv_file.py file would then call your reader and define the ${DATA} variable, like for example:
from mylib import read_csv_file
def get_variables(args):
data = { "DATA": read_csv_file(args) }
return data
See Variable files

How to create a keyword with an optional argument that defaults to None in Robot Framework?

How do I create a custom keyword in Robot Framework that takes an optional argument so that I can call that keyword either with or without argument? e.g. that argument should default to None.
Use ${arg_name}=${None}
Example:
*** Settings ***
Library REST
*** Keyword ***
POST /endpoint
# [Arguments] ${body}=NONE # BAD IDEA
[Arguments] ${body}=${None} # BETTER
&{response}= REST.POST /endpoint ${body}
Now in your test cases you can call POST /endpoint w/ or w/o argument
POST /endpoint
# or
POST /endpoint {"some": "valid json"}
Example:
*** Test Cases ***
My Cool Test Case
[Tags] cool
POST /endpoint
POST /endpoint {"Best Test Automation Framework": "Robot Framework"}
NOTE: don't use NONE since that will set the value to a string 'NONE'
Very basic example using built-in variable ${NONE}:
*** Test Cases ***
Call Keyword Without Argument
Log Name
Call Keyword With Argument
Log Name Steve
*** Keywords ***
Log Name
[Arguments] ${name}=${NONE}
Log Your name is '${name}'
Note: Consider that according to Robot Framework documentation ${NONE} and ${NULL} are synonyms
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#boolean-and-none-null-variables

Use "run keyword if" to call a function with a string argument, but without altering the string

I ran into an odd issue so I wrote this example where I call "print ${dir}" twice:
*** Variables ***
${dir} = "c:\\temp"
*** Test Cases ***
Test
print ${dir}
run keyword if 1 == 1 print ${dir}
*** Keywords ***
print ${input1}
log to console \r${input1}
Output:
"c:\temp"
"c: emp"
What do I need to do to make "print ${dir}" print the same thing each time?
The problem stems from the fact you're using the embedded argument syntax. In order for robot to know what keyword to call, it must first do expansion of the variable before calling the keyword. That removes one layer of backslashes. Then, when your keyword passes what's left to the log to console keyword it sees \t as a tab character, which is why you see a tab character rather than the backslash and the letter "t".
One solution is to use traditional arguments rather than embedded arguments. The following example gives the same output for both times the keyword is called:
*** Variables ***
${dir} = "c:\\temp"
*** Test Cases ***
Test
print ${dir}
run keyword if 1 == 1 print ${dir}
*** Keywords ***
print
[Arguments] ${input1}
log to console \r${input1}
It seems to me that the Run Keyword If keyword does some additional escaping of the backslashes. By adding 1 more backslash you'll see it happen in the first example as well.
To overcome this issue is to switch from backslashes () to forward slashes (/). This works on both *nix and Windows based systems.
*** Variables ***
${dir} = "c:\\temp"
to
*** Variables ***
${dir} = "c:/temp"
This still makes a valid path on Windows. So functionally the path reference will work as well.

Resources