This article will be short because the topic is fairly simple.
Resource and Library are two important Robot Framework concepts. They let us spread one test file across several files, which makes test cases easier to manage and maintain. I will write about applying POM (Page Object Model) later; in Robot Framework, Resource and Library are the pieces used to build it.
Library
A Library file is, at heart, a file that defines keywords. It is a Python file containing keyword definitions as functions.
For example, the Standard Library includes:
- BuiltIn
- Collections
- DateTime
- Dialogs
- OperatingSystem
- Process
- Screenshot
- String
- Telnet
- XML
These standard libraries wrap Python modules and provide basic actions such as checking, comparing, working with files and working with the operating system.
We declare them like this:
*** Settings ***
Library Collections
Library DateTime
Library Dialogs
Library OperatingSystem
# ...
The corresponding Library files are Python files containing keyword definitions: Collections.py, DateTime.py, Dialogs.py, OperatingSystem.py, and so on.
You can also write your own libraries and use them in a test case. For example, my_library.py might contain:
def my_keyword():
print("This is my keyword")
Then declare it in the test case:
*** Settings ***
Library my_library.py
*** Test Cases ***
Test Case 1
My Keyword
The naming rule for calling Python functions from a Robot file is that my_keyword becomes My Keyword: capitalise the first letter of each word and replace the underscores with spaces. For consistent naming, write Python function names in lowercase and separate words with underscores.
There are also external libraries such as SeleniumLibrary and AppiumLibrary. The idea is the same as for a user-created Library; they are simply already written and packaged, so you only need to import one:
*** Settings ***
Library SeleniumLibrary
A Library file must satisfy these conditions:
- Its name must end in
.py; it must really be a Python file. - It must be valid Python. When Python imports it, the file runs once. If it has a syntax error, an import error, or another problem, the import fails; VS Code will report that the Python file cannot be imported even when the path is correct.
Resource
A Resource is a file containing Keywords, Test Cases, Variables and other things you want to use in your test case.
A resource file is a Robot file. Suppose the main test is test.robot and the resource is resource.robot:
# resource.robot
*** Settings ***
Library SeleniumLibrary
*** Keywords ***
Open Google
Open Browser http://google.com chrome
Use it in test.robot; by default both files are in the same folder:
# test.robot
*** Settings ***
Library SeleniumLibrary
Resource resource.robot
*** Test Cases ***
Test Case 1
Open Google
If the files are in different folders, declare an absolute or relative path to the resource. Prefer a relative path: when the project moves to another machine, it will not break in the way an absolute path can.
For example, ./resources/resource.robot means that resource.robot is in a resources directory alongside test.robot.
../resources/resource.robot means that resource.robot is in the resources directory one level above test.robot.
A resource file must satisfy these conditions:
- Its name must end in
.robot. - It may contain sections such as
Settings,Variables,Keywordsand comments. It must never contain aTest Casessection, because a resource is not meant to run test cases.
Variables
A Variables file contains variables. It is used most often in browser automation, where locators for a page are kept in a Python or Robot file.
For example, a Python variables file might contain:
# variables
username = "duyvan"
password = "123456"
Import it with Variables in *** Settings ***:
*** Settings ***
Variables variables.py
Suppose the username field has the ID username and the password field has the ID password. Use the variables like this:
*** Test Cases ***
Test Case 1
Input Text id=username ${username}
Input Text id=password ${password}
${username} is Robot Framework's variable syntax. I will cover it properly in another article.
A Variables file must satisfy these conditions:
- Its name must end in
.py, so it must be a Python file. It can also be a Robot file containing only aVariablessection. - It must not contain syntax errors. Import errors are less of a concern here because a variables file normally imports no libraries; it just contains assigned values.