How do I use same variable in two Test Case in One script?

I am trying to pass the value of one variable from one Test Case in a script to Another Test Case in the same script? I have used ${grabv1} to reuse in the second Test Case. In my case, the value of ${grabv1} seem to be empty.
If anyone came across such situation please let me know.
Thanks in advance.

*** Settings ***
Library RequestsLibrary
Library Collections
Library OperatingSystem
Library String
Library JSONLibrary

*** Variables ***
${API_Base_Endpoint} --------- link to the host ------
${Token} Bearer ADMIN
${Create} mutation{ create (realm: “test_realm”, owner: “test_owner”) { id token realm } }
${grabv1}

*** Test Cases ***
Revoke A User
create session API_Testing ${API_Base_Endpoint}
${headers}= create dictionary Authorization=${Token} Content-Type=application/graphql
${response}= post request API_Testing /graphql data=${Create} headers=${headers}
${contents}= to json ${response.content}
${grab_token}= get value from json ${contents} $.data.create.token
log to console ${grab_token}
${GRAB_TOKEN_STRING}= convert to string ${grab_token}
${GRAB_TOKEN_STRING}= remove string ${GRAB_TOKEN_STRING} [ ] ’
${grabv}= set variable “${GRAB_TOKEN_STRING}”
set global variable ${grabv1} ${grabv}
log to console ${grabv}

*** Variables ***
${API_Base_Endpoint1} ---------link to the host---------
${Token1} Bearer ADMIN
${grabv}
${Revoke0} mutation{ revoke (token: ${grabv1} , reason: “nono”) { id token reason } }

*** Test Cases ***
Revoke 01

create session          API_Testing             ${API_Base_Endpoint}

${headers1}= create dictionary Authorization=${Token} Content-Type=application/graphql
${response1}= post request API_Testing /graphql data=${Revoke0} headers=${headers1}
${contents_revoked}= to json ${response1.content}
log to console ${Revoke0}
log to console ${contents_revoked}

You seem to be using two different test suites files.
The values and scope of variables are only valid when they are in a same test suite file.

If you really need to save variable values, you can pass them to environment variables or store in .py file that another test suite will use.

1 Like

Actually I would need the value extracted from the first test case and use it in the second one in the same execution. How do I do that? I tried to do that in the past under same test case. I end up getting errors when passing the variable in the “request” of the second variable.
I am stuck at this point. can you please help me rewrite the script so that I understand and use in different scenarios.
“Getting a value from a request and store that value in a variable. Then use that value of the variable in another variable to send another request”

Thank you.

A test suite file should have only one Variables, Settings, Test Cases, Keywords, sections.

Then you can try the Set Suite Variable (valid for after setting the value). But I thinks that Set Global Variable should have also worked. You need to confirm in User Reference doc.

Hi,
I have created 1 test case according to what you said and used set global variable. Yet I get the following error:
Revoke A User | FAIL |
ValueError: too many values to unpack (expected 2)

Script:
*** Settings ***
Library RequestsLibrary
Library Collections
Library OperatingSystem
Library String
Library JSONLibrary

*** Variables ***
${API_Base_Endpoint} --------------------------
${Token} Bearer ADMIN
${Create} mutation{ create (realm: “testrealm”, owner: “testowner”) { id token realm } }
${GRABV1}

*** Test Cases ***
Revoke A User
create session API_Testing ${API_Base_Endpoint}
${headers}= create dictionary Authorization=${Token} Content-Type=application/graphql
${response}= post request API_Testing /graphql data=${Create} headers=${headers}
${contents}= to json ${response.content}
${grab_token}= get value from json ${contents} $.data.create.token
log to console ${grab_token}
${GRAB_TOKEN_STRING}= convert to string ${grab_token}
${GRAB_TOKEN_STRING}= remove string ${GRAB_TOKEN_STRING} [ ] ’
${grabv}= set variable “${GRAB_TOKEN_STRING}”
set global variable ${GRABV1} ${grabv}
log to console ${GRABV1}
${Revoke0}= set variable mutation{ revoke (token: ${GRABV1} , reason: “nono”) { id token reason } }
${response1}= post request API_Testing /graphql data=${Revoke0} headers=${headers}
log to console ${Revoke0}
${contents_revoked}= to json ${response1.content}
log to console ${contents_revoked}

Check the correct syntax Set Suite Variable example.

I think I used something similar once. I don’t know if it might be considered a good solution, but here it goes…

You can create a python library with the variable ROBOT_LIBRARY_SCOPE as GLOBAL then I believe it will be the same in the whole execution.

The documentation page has an example about how to do it:
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#library-scope

You could create a special keyword to set the value and another one to get it. And I strongly recommend the usage of keyword decorator in this case:
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#keyword-decorator

1 Like