How to get variable from one test into the other test to use?

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

image

Did you tried Set Suite Variable ?

1 Like

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 is not working since I am running first only the Test1 its okay but then when I run only the Test2 the error is same

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

image

As you can here, first I am running Start ControlDesk only,

then I am running Open ControlDesk Project and Experiment only.

Here in the later case I am getting the error since the variable ${application} is not defined for second test when i run it seperatly.

Yes, can you show me how can i set up that ?

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.

See documentation for using:
Set Suite Variable
Set Global Variable

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.

Hi _daryl

I followed your advise and in test case 2 the result is empty,since
*** Variables ***
${MY_GLOBAL_VAR}

has no value

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.

Hi Shiv,

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

Hope this helps,

Dave.

Hello Dave,

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 ?

And is it possible to add more keywords in Test Setup ?

Is there any documentation about these in built keywords like how to use them and what should be it’s syntax ?

Hi Shiv,

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.

Hope this helps,

Dave.