Can Robot Framework Support External Variables? - robotframework

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!

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}.

Related

Hydra - Specifying config group with a keyword other than the directory

Say I have the following file structure:
|--configs
| |--config.yaml
| |--A
| |--conf1.yaml
| |--conf2.yaml
| |--conf3.yaml
|--main_app.py
and my base config.yaml looks like
defaults:
A: conf1
If I want to run some other config like conf2 or conf3, I can simply run
python main_app.py A=conf2
What I am trying to do is to somehow use a different keyword from the directory name to specify this config above. For example,
python main_app.py B=conf2
For reasons I can't explain, it is not possible for me to change the directory name from A to B. However, I would find the behavior I described above desirable since B is a way more descriptive keyword than the directory name A. Is something like this possible? Thank you!
If you are using Linux you can create a symbolic link.
Hydra itself does not support this.

Can you split keyword into multiple lines

I was trying to implement a keyword with arguments embedded in keyword, in a way that it is shown in Gherkin example. I have a keyword with multiple arguments. I was wondering if it is possible to split it to multiple lines? I wasn’t able to find this in the user guide. I tried to split as if it was a documentation, but with no luck.
*** Test Cases ***
test
When very long keyword ${argument_1}:${argument_2} with
... multiple arguments ${argument_4} is set to ${argument_5}
*** Test Cases ***
test
When very long keyword ${argument_1}:${argument_2} with \
... multiple arguments ${argument_4} is set to ${argument_5}
What is the standard solution here? Should I generally make shorter keywords?
Following your code to the letter, you basically have multiple errors of input separation.
Try something like this:
*** Test Cases ***
test 1
When very long keyword ${argument_1} ${argument_2}
... ${argument_4} ${argument_5}
test 2
When very long keyword ${argument_1} ${argument_2}
... ${argument_4} ${argument_5}
*** Keywords ***
When very long keyword
[Arguments] ${arg1} ${arg2} ${arg3}
... ${arg4} # You can also have new lines in arguments input!
Log My 4 arguments are: ${arg1}, ${arg2}, ${arg3}, ${arg4}
You can split long keywords with And like this:
*** Test Cases ***
Valid Login
Given login page is open
When valid username and password are inserted
And credentials are submitted
Then welcome page should be open
Cucumber has this option to split arguments but I never tried with RobotFramework.
Given the following people exist:
| name | email | phone |
| Aslak | aslak#email.com | 123 |
| Joe | joe#email.com | 234 |
| Bryan | bryan#email.org | 456 |

How to replace text / string in a file with robotframework

I have copied a file in my current working directory. This file has a text:
order value=78
I want to replace this text with new one
parcel value= 500
How can I do this?
Using Get File from OperatingSystem Library you can read the contents of the file,
and using Replace String from String Library you can replace the string and
using Create File from OperatingSystem library you can create the file.
If the file, in which you want to make replacement, is large AND you don't want to load the whole file contents in a variable, then you can make use of Run keyword from OperatingSystem Library. The keyword will execute the shell command, here it is "sed" and make the replacement.
(Of course, you should be on Linux in this case)
For example:
*** Settings ***
| Library | OperatingSystem
*** Test Cases ***
| Example of replacing the text in a file
| | ${result}= | Run | sed -i 's/foo/bar/g' myfile.txt
The variable result will hold the stdout which you get after running the shell command.
I am using a python script for this. Put the code below in a .py file and load this in your robot file using Library in the settings part.
Next in the robot file use this keyword as:
Replace line in file | file | searchExp1 | replaceLine
def Replace_line_in_file(file,searchExp1,replaceLine):
""" Open a file (like input.txt) and find the line that
contains the string searchExp1.
and replace that complete line by replaceLine.
When there are multiple lines that contain searchExp1
then all those lines will be replaced
"""
for line in fileinput.input(file, inplace=1):
if searchExp1 in line:
line = replaceLine+'\n'
sys.stdout.write(line)
I tried using the above keyword "Replace line in file".
But I get the error: Exception TypeError: TypeError("'NoneType' object is not callable",) in > ignored
What other changes are required.

Setup/Teardown Robot Framework more than one keyword?

Is it possible to add to test Setup/Teardown procedure in Robot Framework more than one keyword?
Yes. Use the Run Keywords keyword, and use the special argument AND to separate your keywords.
For example:
*** Settings ***
| Test Setup | Run keywords
| ... | A keyword | with | arguments
| ... | AND | Another keyword | with | arguments
| ... | AND | One more keyword
Note that my use of the pipe-separated format isn't intended to imply that it's the only format you can use in this situation. You can use any supported format with this solution. For example, here is the same thing using the space-separated format:
*** Settings ***
Test Setup Run keywords
... A keyword with arguments
... AND Another keyword with arguments
... AND One more keyword

Looping through the content of a file in RobotFramework

How can I loop through the contents of a file within Robot Framework?
My file contents would be like this:
1001
1002
1003
1004
I want to read the contents one by one, assign it to a variable and then do some operations with it.
Robotframework has several built-in libraries that add a lot of functionality. Two that you can use for this task are the OperatingSystem library and the String library.
You can use the keyword Get File from the OperatingSystem library to read the file, and you can use the Split to Lines keyword from the String library to convert the file contents to a list of lines. Then it's just a matter of looping over the lines using a for loop.
For example:
*** Settings ***
| Library | OperatingSystem
| Library | String
*** Test Cases ***
| Example of looping over the lines in a file
| | ${contents}= | Get File | data.txt
| | #{lines}= | Split to lines | ${contents}
| | :FOR | ${line} | IN | #{lines}
| | | log | ${line} | WARN
This solve my issue same like yours !
${File}= Get File Path\\FileName.txt
#{list}= Split to lines ${File}
:FOR ${line} IN #{list}
\ Log ${line}
\ ${Value}= Get Variable Value ${line}
\ Log ${Value}
I am reading from 'text' file and 'Get Variable Value' is part of builtin library. Thanks!
Below is a list of different examples how to use FOR & While loops in your Robot Framework Test Cases.
http://robotframework.googlecode.com/svn/tags/robotframework-2.5.3/atest/testdata/running/for.txt
My strategy, that I've used successfully with .csv files, would be to create a Python-based keyword that will grab the nth item in a file. The way I did it involved importing the CSV Python library, so to give a more complete answer I'd have to know what file type you're trying to read from.

Resources