Can Robot Framework Support External Variables?

3k views Asked by At

I am trying to create a central file that houses all the variables that we use in our test cases (for instance, the ${BROWSER} variable).

I have also created a central file for a lot of custom defined keywords and I am trying to use the central variable file as a resource for the central keywords file...modularization so to speak

The keywords however do not run as the variables from the central file are not being recognized.

So my question is, has anyone achieved calling external variables in a RF test resource/case successfully? And if so, could you please explain how you did it?

Thank you!

1

There are 1 answers

1
Bryan Oakley On BEST ANSWER

There are several ways, all documented in the Robot Framework Users Guide.

Using command line arguments

You can define variables on the command line using command line options (--variable) option. For example:

pybot --variable FOO:hello mysuite.robot

You can define multiple variables by putting the variables in an argument file, and then you can include the argument file on the command line with the --argumentfile option.

For example, you could create a file named "variables.args" that looks like this:

--variable FOO:Hello
--variable BAR:World

You could then use this file on the command line like this:

pybot --argumentfile variables.args mysuite.robot

My team relies on this heavily. We have two classes of argument files: environments and scenarios. In an environment file we put URLs, usernames, passwords, etc. that are unique to that environment. In the scenario file we'll put things unique to that scenario, such as the definition of ${BROWSER}, the suites to run, etc. Then, our pybot command is very simple: `pybot --argumentfile environment/qa1.args --argumentfile scenarios/chrome_smoke_test.args

Using variable files

If your central file is a python script, you can access all the variables by including the file in your settings. These are called variable files.

For example, you could create a filenamed "variables.py" that looks like this:

FOO = "Hello"
BAR = "World"

You could then use that file in a test suite like this:

*** Settings ***
| Variables | variables.py

*** Test Cases ***
| Example
| | Should be equal | ${FOO} | Hello
| | Should be equal | ${BAR} | World

Using Resource files

Another method is to define your variables in resource files. Resource files allow you to use robot syntax to define variables.

For example, you could create a file named "variables.robot" like this:

*** Variables ***
| ${FOO} | Hello
| ${BAR} | World

You would then include it in a test like this:

*** Settings ***
| Resource | Variables.robot

*** Test Cases ***
| Example
| | Should be equal | ${FOO} | Hello
| | Should be equal | ${BAR} | World

Using environment variables

Another way to use external variables is to define environment variables. Environment variables can be accessed using a % rather than $ when accessing the variable.

For example, assuming you've defined the environment variable HOME, you can access it within in your test as %{HOME}.