Robot: Set and use a local list in Test Cases - robotframework

I need to set a list inside a test case and use this list in [SETUP] to pass this list to a python script, how can I achieve this?
TEST-List
#{lst} Create List a b
#{tmp} Set Test Variable #{lst}
[Setup] Receive List ${tmp} ${another_var}
When I try the code above, I got this error:
Variable '{${tmp}' not found.

[Setup] setting is used for performing actions before a test case. Its purpose is to set up a state for your test. That implies that it happens (executes) before test steps, regardless of where you type it.
In your case, [Setup] Receive List ${tmp} ${another_var} is executed first, and ${tmp} variable has not been declared yet.
The solution might be to move the declaration of ${tmp} to a suite level.

In your example, the code in [Setup] is run before any other code in the test. Therefore, #{lst} and #{tmp} are undefined at the time that it runs.
The simplest solution is to create a local keyword that performs everything you need in the setup, and then call that keyword from [Setup].
Example
*** Keywords ***
Initialize test
#{lst} Create List a b
#{tmp} Set Test Variable #{lst}
Receive List ${tmp} ${another_var}
*** Test Cases ***
TEST-List
[Setup] Initialize test
# ... rest of your test goes here ...

Related

how to pass variable with value from one robot file to another robot file in robot frame work

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

Is it possible to give arguments inside variables in robot framework?

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.

Robotframework Change testcase name with variable

Is there any possible to change testcase name with variable like below?
(I don't want to change name from python side)
*** Variables ***
${country} US
*** Test Cases ***
test_${country}
As far as I know, it isn't possible to use a variable inside a test case name. It follows the same logic as normal Python functions, so normally, it isn't possible.
Instead, you can use the variable in the setup or in the test case directly to modify it's behaviour.
If you want to generate test cases based on a variable, you can write a (python) script that can generate the needed file/test cases with the corresponding values. Or, even better, use an Model-Based Testing tool to produce them.
Yes, you can. The way you have shown it should work. Are you facing any issue with that? If yes, pls provide the detailed error.
Yes this is supported.
example:-
*** Test Cases ***
Test title ${name}
[Tags] DEBUG
Log Welcome ${name}
output:-
robot --variable name:sample eg.robot

How to use "get library instance" with global variable in robot framework

I have to define two variables ${p1} and ${p2} whose scope should be global means they can be use in various teat cases in a single test suite.
when I am doing the below activity inside test case it is working fine:
${p1}= GET LIBRARY INSTANCE P1
${p2}= GET LIBRARY INSTANCE P2
But when I am assigning p1 and p2 as global, I am not able to get the desired result:
set Suite Variable ${p1}= GET LIBRARY INSTANCE P1
set Suite Variable ${p2}= GET LIBRARY INSTANCE P2
I did not want to write ${p1}= GET LIBRARY INSTANCE P1 line in all test cases, what should I do? Any help will be appreciated.
You should define a Suite Setup in which you could set your variables for the whole suite. You should get your library instances first and then simply set those variables as suite variables like it is shown in the example.
${ID} = Get ID
Set Suite Variable ${ID}
In your case it should look like something this:
*** Settings ***
Suite Setup Setup Global Variables
*** Keywords ***
Setup Global Variables
${p1}= GET LIBRARY INSTANCE P1
${p2}= GET LIBRARY INSTANCE P2
Set Suite Variable ${p1}
Set Suite Variable ${p2}
*** Test Cases ***
Test CaseA
Log ${p1}
Log ${p2}
Test CaseB
Log ${p1}
Log ${p2}
Note that these variables will be accessible only in this suite file.
You are using invalid syntax. The documentation for Set suite variable says that it takes a variable name as the first argument and one or more values (not a keyword) as subsequent arguments. You are giving the string ${p1}= GET LIBRARY INSTANCE as the variable name, and the string P1 as the value.
The correct form is like the following. Because ${p1} exists locally you do not need to specify it when calling set suite variable.
${p1}= GET LIBRARY INSTANCE P1
set Suite Variable ${p1}

Robot: assign variables in setup phase using Run Keywords

I'm trying to create a setup phase for a test case in which I assign variables. I know in order to do multiple keywords I need to use Run Keywords, but is it possible to set variables when doing this? For example:
*** Test Cases ***
Case1
[Setup] Run Keywords
... ${var1}= Keyword1
... AND ${var2}= Keyword2
obviously the above doesn't work because ${var1} and ${var2} are just treated as arguments to Run Keywords. Since they haven't been defined yet, setup fails.
No, you cannot. Even though you added "using Run Keywords", this question has the same answer as Is possible to create new variable in suite/test set up - Robot Framework?
You can use the Set Suite Variable keywork to do that.
set suite variable ${var1} Hello World
You might need to escape the variable...
set suite variable \${var1} Hello World
From the builtin library documentation:
If a variable already exists within the new scope, its value will be overwritten. Otherwise a new variable is created. If a variable already exists within the current scope, the value can be left empty and the variable within the new scope gets the value within the current scope.
The question here why are you trying to do this?
The way I do it, if I want to call keywords and set their outputs in variables
to reuse them in my test suite, I do the following:
*** Settings ***
Library BuiltIn
Suite Setup Initialize Variables
*** Keywords ***
Initialize Variables
${Argument1} = Set Variable some_value
${output1} = Keyword1 ${Argument1}
${output2} = Keyword2
${output3} = Keyword3 ${Argument1} other_value
*** Test Cases ***
Test Case 1
# Here you can use the variables that you initialized in the Suite Setup.
Log ${output1}
Log ${output2}
Log ${output3}
Test Case 2
# Also here you can use the same variables.
No Operation
Note: If you want to setup the variables for each test case, you can do it either in the settings section like this:
*** Settings ***
Test Setup Initialize Variables
Or you can use the setting in the test case itself (same as what you did in your question)
Test Case 1
[Setup] Initialize Variables
Note that "Initialize Variables" can take arguments as well if you need.

Resources