Suppose I have a Test Suite and inside that I have two tests Test1 and Test2.
Now, first I am running only Test1 and here I am geting one variable as an output and then I am setting the variable as global variable.
Now, afterwards I am running second Test2 and I want to use the variable from first Test1 into Test2.
So, how can I make it possible ?
Example Test suite is as below,
Test Suite
Test1
Start Application
${application}= Switch On Application
Set Global Variable ${application}
Test2
Open Project and Experiment
Open Project ${application} ${Project_name} ${Project_path}
Open Experiment ${application} ${Experiment_name} ${Project_path}
The error I am getting is this after running Test2
Basically your error is saying variable ${application} doesnât exists, setting a Global Variable takes two values âNameâ and âValuesâ
So if you was to create a *** Variables *** section at top of the file to see it working, lets say ${APPLICATION_GLOBAL} and then your Set Global Variable would look like this:
Set Global Variable ${APPLICATION_GLOBAL} ${application}
However using Set Suite Variable as mentioned above is probably worth exploring as it does as you describe but more controlled by making a variable available everywhere within the scope of the current suite, to oppose to the global variable where it makes a variable available globally in all tests and suites.
This would be correct because in doing it this way either via global or suite variable, if test2 is ran on its own then test2 has a dependency on test1 so the variable would never hold a value.
All things to think about when using variables in this way, and how you structure your suite and tests.
What you could look into is Test Setup and Suite Setup which would be a great use case to shift the application startup to, if all test in said suite require this as a pre setup
Just to note the Set Suite Variable still doesnât show correctly, you need to pass âNameâ and âValueâ, name being the Variable the value is assigned to, and then second is value to be assigned.
Anyway, to remove the dependency from test 1 (which looks to be a pre-setup task, given it just assigns a variable once returned for the KW) you can use Test Setup or Suite Setup as mentioned, for example for test setup:
*** Settings ***
Documentation New test to open controldesk application
Resource List_of_keywords.resource
Default Tags postive
Library Library_robot.py
Test Setup Start ControlDesk
*** Variables ***
${APP}
*** Keywords ***
Start ControlDesk
${application}= Swicth On ControlDesk
Set Suite Variable ${APP} ${application}
*** Test Cases ***
Open Controldesk Project and Experiment
Open Controldesk Project ${APP}
Open Controldesk Experiment ${APP}
In the above, given I cant see further down the code if thereâs other tests Iâve just adjusted based on whatâs seen, the Test Startup is executed before a test case and the two keywords are ran.
Just a note, I cannot run this locally as these keywords are not my own, I believe Iâve copied word for word of your keywords.
I think once you understand the usage better of Global/Suite Variable and Test/Suite Setup youâll have no problems at all, links above will take you to the documentations for these
I have the same issue with Global Variable
I am using all of this
*** Test Cases ***
Test Case 1
${my_local_var} Set Variable "I'm a local Variable"
Set Global Variable ${my_local_var}
Set Suite Variable ${my_local_var}
Set Test Variable ${my_local_var}
but in Test Case 2 ${my_local_var} is not found
Test Case 2 | FAIL |
Variable â${my_local_var}â not found.
Hi @samsanati2000
You need to pass two values to Set Global Variable, the name of the variable (the variable that will be used throughout various tests, global variables are normal denoted by keeping them uppercase and sit at the top of the file) and the values to be assigned. I link above them both to links to the documentation for them.
For a basic example: *** Variables *** ${MY_GLOBAL_VAR}
And then to assign to it Set Global Variable ${MY_GLOBAL_VAR} ${my_local_var}
Hope that helps, unfortunately Iâm out at the moment.
Aha yes it will be if nothing is being assigned, and Iâd assume testcase one or a testcase other than two is where you are assigning it, Iâda guessâŠ.
So assuming testcase one is fine as this is where you hold the assigning of the global variable and two is not, so would I be right in assuming you are running these separately? If so then Youâd probably want to make use of Test/Suite Setup keywords (I mention this above with an example as an approach) and manage the assignment to the global/suite variable for which ever approach you take there, as this one way of doing it with the details youâve given.
Definitely worth looking at the documentation surrounding these keywords as these will help you understand what they do, and their usings.
After reading through this thread, I have a question:
Are you running you running a single test then after checking the result and some time later running the second test (different robot processes)? Or are you running all the tests in the robot file together (same robot processes)?
If you are running them as separate tests (different robot processes) then setting a global or suite variable wonât help you, global and suite variables only work when itâs the same robot process.
If Iâm right and you are running them as separate tests, then some suggestions on how to handle it:
you could use Dialogs Library to pause the execution, do your manual checks before continuing to the next test case, this way you can run them in the same process but still have the test sop so you can check things. Execute Manual Step will give you pass fail buttons, so you could fail the test to stop the execution
You could write the variable values to a file and then for the next test read them from that file, this way the second test running in a separate process can still use the value from the first test.
Like the file method you could also set the value to an OS level environment variable, however this is quite volatile and wonât always work, but you cane see Set Environment Variable and Get Environment Variable
Can you please give me an example of the second approach how can I make that file and assign the variable to that file and how can I read the variable from that file ?
Iâll start with the question you asked @_daryl because itâs important for my answer
In the Settings Section you can have multiple Library lines for each of the libraries you want to include in your test case
e.g.:
*** Settings ***
Documentation New test to open controldesk application
Resource List_of_keywords.resource
Default Tags postive
Library Library_robot.py
Library OperatingSystem
Test Setup Start ControlDesk
*** Variables ***
Youâll see here Iâve included the OperatingSystem Library whilch Iâll mention keywords from this library in my answer.
I donât believe there is a is an âOfficialâ way to do this, but itâs quite easy if you only have one or a small number of variables to persist across tests.
A very simple way to handle this, say you have âTest Case Aâ and it collects a variable ${examplevar} with a value of âmyvalueâ, you could use Create File to create a file with the filename of the variable and the only content in it being the variable value, then in âTest Case Bâ or âTest Case Câ you could read the content of that file back into ${examplevar} with Get File
Something like this
*** Test Cases ***
Test Case 1
${my_local_var} Set Variable "I'm a local Variable"
Create File ${CURDIR}${/}my_local_var.txt ${my_local_var}
Test Case Z
${my_local_var} Get File ${CURDIR}${/}my_local_var.txt
Log ${my_local_var} # should return "I'm a local Variable"
If your variable contains a object data you may need to use Create Binary File and Get Binary File, and even then, depending on what the object is it still might not work, but for numbers and strings this approach will work.
If you have many variables this could get messy quickly and youâd probably want a different approach like maybe putting the variables in a dictionary then using JSONLibraryâs Dump Json To File and Load Json From File
The Resources section of the Robot Framework homepage has links to many of the commonly used libraries but if you canât find what your after you can just use google for the thing you want with robot framework and youâll often find a suitable library, You can use keywords from several libraries in the same test case or even the same keyword even passing variables from one libraries keyword to another libraries keyword to acheive what ever your test need to do, this is what makes robot framework so powerful.