2.4 Using test libraries
Test libraries contain those lowest-level keywords, often called library keywords, which actually interact with the system under test. All test cases always use keywords from some library, often through higher-level user keywords. This section explains how to take test libraries into use and how to use the keywords they provide. Creating test libraries is described in a separate section.
- 2.4.1 Taking test libraries into use
- 2.4.2 Setting custom name to test library
- 2.4.3 Standard libraries
- 2.4.4 External libraries
2.4.1 Taking test libraries into use
Instructions for taking test libraries into use are given in the subsections below.
Using Library setting
Test libraries are normally imported using the Library setting in the Setting table and having the library name in the subsequent column. The library name is case-sensitive (it is the name of the module or class implementing the library and must be exactly correct), but any spaces in it are ignored. With Python libraries in modules or Java libraries in packages, the full name including the module or package name must be used.
In those cases where the library needs arguments, they are listed in the columns after the library name. It is possible to use default values, variable number of arguments, and named arguments in test library imports similarly as with arguments to keywords. Both the library name and arguments can be set using variables.
Importing test libraries | |||
---|---|---|---|
Setting | Value | Value | Value |
Library | OperatingSystem | ||
Library | com.company.TestLib | ||
Library | MyLibrary | arg1 | arg2 |
Library | ${LIBRARY} |
It is possible to import test libraries in test case files, resource files and test suite initialization files. In all these cases, all the keywords in the imported library are available in that file. With resource files, those keywords are also available in other files using them.
Using Import Library keyword
Another possibility to take a test library into use is using the keyword Import Library from the BuiltIn library. This keyword takes the library name and possible arguments similarly as the Library setting. Keywords from the imported library are available in the test suite where the Import Library keyword was used. This approach is useful in cases where the library is not available when the test execution starts and only some other keywords make it available.
Using Import Library keyword | ||||
---|---|---|---|---|
Test Case | Action | Argument | Argument | Argument |
Example | Do Something | |||
Import Library | MyLibrary | arg1 | arg2 | |
KW From Mylibrary |
Library search path
The most common way to specify a test library to import is using its name, like it has been done in all the examples in this section. In these cases Robot Framework tries to find the class or module implementing the library from the library search path. Basically, this means that the library code and all its possible dependencies must be in PYTHONPATH or, when running tests on Jython, in a CLASSPATH. Setting the library search path is explained in a section of its own. Libraries can also set the search path automatically or have special instructions on how to do it. All standard libraries, for example, are in the library search path automatically.
The biggest benefit of this approach is that when the library search path has been configured, often using a custom start-up script, normal users do not need to think where libraries actually are installed. The drawback is that getting your own, possible very simple, libraries into the search path may require some additional configuration.
Using physical path to library
Another mechanism for specifying the library to import is using a path to it in the file system. This path is considered relative to the directory where current test data file is situated similarly as paths to resource and variable files. The main benefit of this approach is that there is no need to configure the library search path.
If the library is a file, the path to it must contain extension. For Python libraries the extension is naturally .py and for Java libraries it can either be .class or .java, but the class file must always be available. If Python library is implemented as a directory, the path to it must have a trailing forward slash (/). Following examples demonstrate these different usages.
Importing test libraries using physical paths to them | |||
---|---|---|---|
Setting | Value | Value | Value |
Library | PythonLib.py | ||
Library | /absolute/path/JavaLib.java | ||
Library | relative/path/PythonDirLib/ | possible | arguments |
Library | ${RESOURCES}/Example.class |
A limitation of this approach is that libraries implemented as Python classes must be in a module with the same name as the class. Additionally, importing libraries distributed in JAR or ZIP packages is not possible with this mechanism.
2.4.2 Setting custom name to test library
The library name is shown in test logs before keyword names, and if multiple keywords have the same name, they must be used so that the keyword name is prefixed with the library name. The library name is got normally from the module or class name implementing it, but there are some situations where changing it is desirable:
- There is a need to import the same library several times with different arguments. This is not possible otherwise.
- The library name is inconveniently long. This can happen, for example, if a Java library has a long package name.
- You want to use variables to import different libraries in different environments, but refer to them with the same name.
- The library name is misleading or otherwise poor. In this case, changing the actual name is, of course, a better solution.
The basic syntax for specifying the new name is having the text WITH NAME (case-insensitive) after the library name and then having the new name in the next cell. The specified name is shown in logs and must be used in the test data when using keywords' full name (LibraryName.Keyword Name).
Importing libraries with custom names | |||
---|---|---|---|
Setting | Value | Value | Value |
Library | com.company.TestLib | WITH NAME | TestLib |
Library | ${LIBRARY} | WITH NAME | MyName |
Possible arguments to the library are placed into cells between the original library name and the WITH NAME text. The following example illustrates how the same library can be imported several times with different arguments:
Importing the same library several times with a different name | |||||
---|---|---|---|---|---|
Setting | Value | Value | Value | Value | Value |
Library | SomeLibrary | localhost | 1234 | WITH NAME | LocalLib |
Library | SomeLibrary | server.domain | 8080 | WITH NAME | RemoteLib |
Test Case | Action | Argument | Argument |
---|---|---|---|
My Test | LocalLib.Some Keyword | some arg | second arg |
RemoteLib.Some Keyword | another arg | whatever | |
LocalLib.Another Keyword |
Setting a custom name to a test library works both when importing a library in the Setting table and when using the Import Library keyword.
2.4.3 Standard libraries
Some test libraries are distributed with Robot Framework and these libraries are called standard libraries. The BuiltIn library is special, because it is taken into use automatically and thus its keywords are always available. Other standard libraries need to be imported in the same way as any other libraries, but there is no need to install them.
Normal standard libraries
The available normal standard libraries are listed below with links to their documentations:
Remote library
In addition to the normal standard libraries listed above, there is also Remote library that is totally different than the other standard libraries. It does not have any keywords of its own but it works as a proxy between Robot Framework and actual test library implementations. These libraries can be running on other machines than the core framework and can even be implemented using languages not supported by Robot Framework natively.
See separate Remote library interface section for more information about this concept.
2.4.4 External libraries
Any test library that is not one of the standard libraries is, by definition, an external library. The Robot Framework open source community has implemented several generic libraries, such as Selenium2Library and SwingLibrary, which are not packaged with the core framework. A list of publicly available libraries can be found from http://robotframework.org.
Generic and custom libraries can obviously also be implemented by teams using Robot Framework. See Creating test libraries section for more information about that topic.
Different external libraries can have a totally different mechanism for installing them and taking them into use. Sometimes they may also require some other dependencies to be installed separately. All libraries should have clear installation and usage documentation and they should preferably automate the installation process.