'How to Access Global Variable in Robot framework
I am trying to use variable which is set in one file and want to access variable in another file. How to do it in Robot framework.
Currently my file test_1.robot has variable
*** Settings ***
Library Collections
*** Test Cases ***
Getting a valid response
Create Http Session rest_session ${service_url}
Set Global Variable ${HEADERS}
Now I want to access ${HEADERS} in another file test_2.robot. In test_2.robot I am trying to print headers to console. But i get no such variable ${HEADERS}.
*** Settings ***
Library Collections
*** Test Cases ***
Getting a valid response
Create Http Session rest_session ${service_url}
Log to console ${HEADERS}
Can someone please tell me what am I missing?
Solution 1:[1]
In your example ${HEADERS} variable is empty you need to assign it to something and create the variable in *** Variables *** section of your file if you want to use in a different file.
This is a simple way to use Global Variables
You can Either use Set Global Variable:
Makes a variable available globally in all tests and suites.
Variables set with this keyword are globally available in all test cases and suites executed after setting them. Setting variables with this keyword thus has the same effect as creating from the command line using the options --variable or --variablefile. Because this keyword can change variables everywhere, it should be used with care.
Set Global Variable ${Header} ${xpath_to_header}
Or you can use Set Suite Variable:
Makes a variable available everywhere within the scope of the current suite.
Variables set with this keyword are available everywhere within the scope of the currently executed test suite. Setting variables with this keyword thus has the same effect as creating them using the Variable table in the test data file or importing them from variable files.
Possible child test suites do not see variables set with this keyword by default. Starting from Robot Framework 2.9, that can be controlled by using children= as the last argument. If the specified is a non-empty string or any other value considered true in Python, the variable is set also to the child suites. Parent and sibling suites will never see variables set with this keyword.
The name of the variable can be given either as a normal variable name (e.g. ${NAME}) or in escaped format as \${NAME} or $NAME. Variable value can be given using the same syntax as when variables are created in the Variable table.
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.
Set Suite Variable ${Header} ${xpath_to_header}
Now you can use your ${Header} variable in your test case. Now you can log the variable as follows
log ${Header}
Full example:
*** Settings ***
*** Keywords ***
Random Global Variable
${Header}= Set Variable xpath=//div...
set suite variable ${Header}
*** Test Cases ***
Example
log to console ${Header}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AutoTester213 |
