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
Related
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}
Test 1.robot
*** Variables ***
${node} babitha
*** Test Cases ***
A Test Case
Log To Console ${node}
Test 2.robot
*** Settings ***
Resource C:\Users\2013\Desktop\Test 1.robot
*** Test Cases ***
A Test Case
Log To Console ${node}
Trying to print babitha on console when i am running Test 2.robot file
A resource file cannot contain *** Test Cases *** section - if you try to run your sample the framework will probably produce an error with such message.
If you want to use a variable from one suite in another, then in the initial one you declare it it as such with the Set Global Variable keyword:
Set Global Variable ${node} # you can reassign its value here, or leave to the previously set
This has one caveat though - you must be sure the setter is going to be called before the case that will try to use it (naturally) - otherwise, it will not be defined for it.
An alternative is to store the variable in a 3rd file (a resource one) and import it in the suites that need it.
And another alternative is to pass it with --variable node:babitha in the command line when running the tests - thus it will be globally available from the start, and all cases will be able to access it (and modify, through the Set Global Variable).
If more .robot files have to share some variable(s), it's better to keep these variables in a separate file (and folder), I'd suggest similar structure:
.
|__Resources
|__Tests
In Resources/, you will have a file I'll call config.py:
node = "babitha"
The last step you need is to load the config file in both of your test suites:
Tests/Test 1.robot
*** Settings ***
Variables ../Resources/config.py
*** Test Cases ***
A Test Case
Log To Console ${node}
Tests/Test 2.robot
*** Settings ***
Variables ../Resources/config.py
*** Test Cases ***
A Test Case
Log To Console ${node}
One remark at the end:
You used an absolute path C:\Users\2013\Desktop\Test 1.robot, try to avoid that, if someone else clones your project, it will likely break on their environment
Library REST ${base_url}
*** Keywords ***
Get Requests
GET ${rest_of_the_url}
Output response body
*** Test Cases ***
Do some searching
Get Requests
*** Variables ***
${base_url} https://business.com
${rest_of_the_url} /api/${department}/${person_name}
How can I assign values to ${department} and ${person_name}? I don't want to set those inside Variables because then I cannot write multiple scenarios inside one .robot file. Is it possible to do the assignment as arguments?
i do not think there is a way to pass arguments within the variables,
The below section is straight from the documentation of Robotframework,
where you can create Variables inside variables
Variables inside variables
Variables are allowed also inside variables, and when this syntax is used, variables are resolved from the inside out. For example, if you have a variable ${var${x}}, then ${x} is resolved first. If it has the value name, the final value is then the value of the variable ${varname}. There can be several nested variables, but resolving the outermost fails, if any of them does not exist.
In the example below, Do X gets the value ${JOHN HOME} or ${JANE HOME}, depending on if Get Name returns john or jane. If it returns something else, resolving ${${name} HOME} fails.
*** Variables ***
${JOHN HOME} /home/john
${JANE HOME} /home/jane
*** Test Cases ***
Example
${name} = Get Name
Do X ${${name} HOME}
E.g.,
${person_name}= Set Variable Matt
${department}= Set Variable R&D
# then your code continues
${rest_of_the_url} /api/${department}/${person_name}
Set Variable documentation.
Try to see this using the Set Test / Suite / Global Variable keywords here:
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html
Use "Set Suite Variable" keyword then enter the variables $ {person_name} e
$ {department} inside * Variables * then you should read the value inside the test.
It would be nice to use a variable when importing resources to test cases. Right now I import them like this:
Resource ${EXECDIR}/resources/RandomPage.resource
I would like to import them like this:
Resource ${RESOURCES}/RandomPage.resource
And there is a Set Global Variable keyword, which could be used to set the ${RESOURCES}, but I can't decide where to put it, Setups do not seem a good place.
Where is a good practice to initialize this variable?
You could pass it as command line argument:
robot -v ${RESOURCES}:<value> <rest of the arguments>
But what problem are you trying to solve?
There are a couple of ways to do this.
First, using the Variables section as below:
*** Settings ***
Resource ${RESOURCE}
*** Variables ***
${RESOURCE} path/to/resource/file
Second, you can create a variables.py file and reference it in the Settings section as follows
variables.py
RESOURCE = 'path/to/resource/file'
test_suite.robot
*** Settings ***
Resource ${RESOURCE}
Variables /path/to/variable.py
From what I read about variable scopes and importing resource files in robotframework doc i would expect this to work (python 2.7, RF 2.8.7):
Test file:
*** Settings ***
Resource VarRes.txt
Suite Setup Preconditions
*** Variables ***
*** Test Cases ***
VarDemo
Log To Console imported [${TODAY}]
*** Keywords ***
Resource file:
*** Settings ***
Library DateTime
*** Variables ***
${TODAY} ${EMPTY} # Initialised during setup, see keyword Preconditions
*** Keywords ***
Format Local Date
[Arguments] ${inc} ${format}
${date} = Get Current Date time_zone=local increment=${inc} day result_format=${format}
[Return] ${date} # formatted date
Preconditions
${TODAY} = Format Local Date 0 %Y-%m-%d
Log To Console inited [${TODAY}]
However the output is:
inited [2015-03-20]
imported []
RF documentation states:
Variables with the test suite scope are available anywhere in the test
suite where they are defined or imported. They can be created in
Variable tables, imported from resource and ....
which I think is done here.
If I add a line to keyword Preconditions like this, it works:
Preconditions
${TODAY} = Format Local Date 0 %Y-%m-%d
Set Suite Variable ${TODAY}
Log To Console inited [${TODAY}]
The reason is that in the first line a local variable is defined, instead of initialising the test suite variable declared in the variable table. A paragraph in RF doc hints to that:
Variables set during the test execution either using return values
from keywords or using Set Test/Suite/Global Variable keywords always
override possible existing variables in the scope where they are set
I think a major drawback of RF is that you cannot define variables dynamically in the variable table. Setting the scope of a variable from within a keyword is something I try to avoid.
For dynamic variables, you might to use a variable file in Python. See the section "Implementing variable file as Python or Java class" in the User Guide.
For example, I use a variables.py with:
if platform.system() in ['Darwin', 'Linux']:
OS_FAMILY = 'unix'
elif platform.system() == 'Windows':
OS_FAMILY = 'windows'
else:
OS_FAMILY = 'unknown'
OS_FAMILY_IS_UNIX = OS_FAMILY == 'unix'
OS_FAMILY_IS_WINDOWS = OS_FAMILY == 'windows'
Then in the Robot Tests I can use the dynamic variables ${OS_FAMILY}, ${OS_FAMILY_IS_UNIX}, and ${OS_FAMILY_IS_WINDOWS} anywhere.
You should be able to do create your ${TODAY} variable.