Creating user keywords¶
Keyword sections are used to create new higher-level keywords by combining existing keywords together. These keywords are called user keywords to differentiate them from lowest level library keywords that are implemented in test libraries. The syntax for creating user keywords is very close to the syntax for creating test cases, which makes it easy to learn.
User keyword syntax¶
Basic syntax¶
In many ways, the overall user keyword syntax is identical to the test case syntax. User keywords are created in Keyword sections which differ from Test Case sections only by the name that is used to identify them. User keyword names are in the first column similarly as test cases names. Also user keywords are created from keywords, either from keywords in test libraries or other user keywords. Keyword names are normally in the second column, but when setting variables from keyword return values, they are in the subsequent columns.
Most user keywords take some arguments. This important feature is used already in the second example above, and it is explained in detail later in this section, similarly as user keyword return values.
User keywords can be created in suite files, resource files, and suite initialization files. Keywords created in resource files are available for files using them, whereas other keywords are only available in the files where they are created.
Settings in the Keyword section¶
User keywords can have similar settings as test cases, and they have the same square bracket syntax separating them from keyword names. All available settings are listed below and explained later in this section.
- [Documentation]
- Used for setting a user keyword documentation.
- [Tags]
- Sets tags for the keyword.
- [Arguments]
- Specifies user keyword arguments.
- [Setup], [Teardown]
- Specify user keyword setup and teardown. [Setup] is new in
Robot Framework 7.0.
- [Timeout]
- Sets the possible user keyword timeout. Timeouts are discussed
in a section of their own.
- [Return]
- Specifies user keyword return values. Deprecated in Robot Framework 7.0,
the RETURN statement should be used instead.
Note
The format used above is recommended, but setting names are
case-insensitive and spaces are allowed between brackets and the name.
For example, [ TAGS ]:setting is valid.
User keyword name and documentation¶
The user keyword name is defined in the first column of the Keyword section. Of course, the name should be descriptive, and it is acceptable to have quite long keyword names. Actually, when creating use-case-like test cases, the highest-level keywords are often formulated as sentences or even paragraphs.
User keywords can have a documentation that is set with the [Documentation] setting. It supports same formatting, splitting to multiple lines, and other features as test case documentation. This setting documents the user keyword in the test data. It is also shown in a more formal keyword documentation, which the Libdoc tool can create from resource files. Finally, the first logical row of the documentation, until the first empty row, is shown as a keyword documentation in test logs.
Sometimes keywords need to be removed, replaced with new ones, or
deprecated for other reasons. User keywords can be marked deprecated
by starting the documentation with *DEPRECATED*, which will
cause a warning when the keyword is used. For more information, see
the Deprecating keywords section.
Note
Prior to Robot Framework 3.1, the short documentation contained only the first physical line of the keyword documentation.
User keyword tags¶
User keywords can be tagged similarly as test cases and library keywords support tags as well.
Setting tags using dedicated settings¶
Similarly as when tagging test cases, there are two settings affecting user keyword tags:
- Keyword Tags setting in the Settings section
- All keywords in a file with this setting always get specified tags.
- [Tags] setting with each keyword
- Keywords get these tags in addition to possible tags specified using the
Keyword Tags setting. The [Tags] setting also allows
removing tags set with Keyword Tags by using the
-tagsyntax.
Keyword tags can be specified using variables, the -tag syntax supports
patterns, and so on, exactly as test case tags.
Note
The Keyword Tags setting is new in Robot Framework 6.0. With earlier versions all keyword tags need to be specified using the [Tags] setting.
Note
The -tag syntax for removing common tags is new in Robot Framework 7.0.
Listing tags in documentation¶
In addition to using the dedicated settings, keyword tags can be specified
as part of the keyword documentation in a Tags: section. Tags must be separated
with a comma and they can be listed either on the same line as the Tags: header
or on subsequent indented lines:
As the latter example demonstrates, the syntax for defining tags is similar to how Libdoc handles argument, return value and exception documentation.
Note
Prior to Robot Framework 7.5, tags were only supported on the last row of the documentation.
Use cases for keyword tags¶
Keyword tags are shown in logs and in documentation generated by Libdoc, where the keywords can also be searched based on tags. The --removekeywords and --flattenkeywords commandline options also support selecting keywords by tag, and new usages for keywords tags are possibly added in later releases.
Similarly as with test case tags, user keyword tags with the robot:
prefix are reserved for special features by Robot Framework
itself. Users should thus not use any tag with these prefixes unless actually
activating the special functionality. Starting from Robot Framework 6.1,
flattening keyword during execution time can be taken into use using
reserved tag robot:flatten.
User keyword arguments¶
Most user keywords need to take some arguments. The syntax for
specifying them is probably the most complicated feature normally
needed with Robot Framework, but even that is relatively easy,
particularly in most common cases. Arguments are normally specified with
the [Arguments] setting, and argument names use the same
syntax as variables, for example ${arg}.
Positional arguments with user keywords¶
The simplest way to specify arguments (apart from not having them at all) is using only positional arguments. In most cases, this is all that is needed.
The syntax is such that first the [Arguments] setting is
given and then argument names are defined in the subsequent
cells. Each argument is in its own cell, using the same syntax as with
variables. The keyword must be used with as many arguments as there
are argument names in its signature. The actual argument names do not
matter to the framework, but from users' perspective they should
be as descriptive as possible. It is recommended
to use lower-case letters in variable names, either as
${my_arg}, ${my arg} or ${myArg}.
Default values with user keywords¶
When creating user keywords, positional arguments are sufficient in most situations. It is, however, sometimes useful that keywords have default values for some or all of their arguments. Also user keywords support default values, and the needed new syntax does not add very much to the already discussed basic syntax.
In short, default values are added to arguments, so that first there is
the equals sign (=) and then the value, for example ${arg}=default.
There can be many arguments with defaults, but they all must be given after
the normal positional arguments. The default value can contain a variable
created on test, suite or global scope, but local variables of the keyword
executor cannot be used. Default value can
also be defined based on earlier arguments accepted by the keyword.
Note
The syntax for default values is space sensitive. Spaces
before the = sign are not allowed, and possible spaces
after it are considered part of the default value itself.
When a keyword accepts several arguments with default values and only
some of them needs to be overridden, it is often handy to use the
named arguments syntax. When this syntax is used with user
keywords, the arguments are specified without the ${}
decoration. For example, the second keyword above could be used like
below and ${arg1} would still get its default value.
As all Pythonistas must have already noticed, the syntax for specifying default arguments is heavily inspired by Python syntax for function default values.
Variable number of arguments with user keywords¶
Sometimes even default values are not enough and there is a need
for a keyword accepting variable number of arguments. User keywords
support also this feature. All that is needed is having list variable such
as @{varargs} after possible positional arguments in the keyword signature.
This syntax can be combined with the previously described default values, and
at the end the list variable gets all the leftover arguments that do not match
other arguments. The list variable can thus have any number of items, even zero.
Notice that if the last keyword above is used with more than one
argument, the second argument ${opt} always gets the given
value instead of the default value. This happens even if the given
value is empty. The last example also illustrates how a variable
number of arguments accepted by a user keyword can be used in a for
loop. This combination of two rather advanced functions can
sometimes be very useful.
The keywords in the examples above could be used, for example, like this:
Again, Pythonistas probably notice that the variable number of arguments syntax is very close to the one in Python.
Free named arguments with user keywords¶
User keywords can also accept free named arguments by having a dictionary
variable like &{named} as the absolutely last argument. When the keyword
is called, this variable will get all named arguments that do not match
any positional argument or named-only argument in the keyword
signature.
The last example above shows how to create a wrapper keyword that accepts any positional or named argument and passes them forward. See free named argument examples for a full example with same keyword.
Free named arguments support with user keywords works similarly as kwargs
work in Python. In the signature and also when passing arguments forward,
&{kwargs} is pretty much the same as Python's **kwargs.
Named-only arguments with user keywords¶
User keywords support named-only arguments that are inspired by Python's
keyword-only arguments.
This syntax is typically used by having normal arguments after
variable number of arguments (@{varargs}). If the keywords does not
use varargs, it is possible to use just @{} to denote that the subsequent
arguments are named-only:
Named-only arguments can be used together with positional arguments as well as with free named arguments. When using free named arguments, they must be last:
When passing named-only arguments to keywords, their order does not matter other than they must follow possible positional arguments. The keywords above could be used, for example, like this:
Named-only arguments can have default values similarly as normal user keyword arguments. A minor difference is that the order of arguments with and without default values is not important.
Argument conversion with user keywords¶
User keywords support automatic argument conversion based on explicitly specified
types. The type syntax ${name: type} is the same, and the supported conversions
are the same, as when creating variables.
The basic usage with normal arguments is very simple. You only need to specify
the type like ${count: int} and the used value is converted automatically.
If an argument has a default value like ${count: int}=1, also the default
value will be converted. If conversion fails, calling the keyword fails with
an informative error message.
Tip
Using Literal, like in the above example, is a convenient way to
limit what values are accepted.
When using variable number of arguments, the type is specified like
@{numbers: int} and is applied to all arguments. If arguments may have
different types, it is possible to use an union like @{numbers: float | int}.
With free named arguments the type is specified like &{named: int} and
it is applied to all argument values. Converting argument names is not supported.
Note
Argument conversion with user keywords is new in Robot Framework 7.3.
Embedding arguments into keyword name¶
The previous section explained how to pass arguments to keywords so that they are listed separately after the keyword name. Robot Framework has also another approach to pass arguments, embedding them directly to the keyword name, used by the second test below:
As the example illustrates, embedding arguments to keyword names can make the data easier to read and understand even for people without any Robot Framework experience.
Basic syntax¶
The previous example showed how using a keyword Select cat from list is
more fluent than using Select from list so that cat is passed to
it as an argument. We obviously could implement Select cat from list
as a normal keyword accepting no arguments, but then we needed to implement
various other keywords like Select dog from list for other animals.
Embedded arguments simplify this and we can instead implement just one
keyword with name Select ${animal} from list and use it with any
animal:
As the above example shows, embedded arguments are specified simply by using
variables in keyword names. The arguments used in the name are naturally
available inside the keyword and they have different values depending on how
the keyword is called. In the above example, ${animal} has value cat when
the keyword is used for the first time and dog when it is used for
the second time.
Starting from Robot Framework 6.1, it is possible to create user keywords that accept both embedded and "normal" arguments:
Other than the special name, keywords with embedded arguments are created just like other user keywords. They are also used the same way as other keywords except that spaces and underscores are not ignored in their names when keywords are matched. They are, however, case-insensitive like other keywords. For example, the Select ${animal} from list keyword could be used like select cow from list, but not like Select cow fromlist.
Embedded arguments do not support default values or variable number of arguments like normal arguments do. If such functionality is needed, normal arguments should be used instead. Passing embedded arguments as variables is possible, but that can reduce readability:
Embedded arguments matching wrong values¶
One tricky part in using embedded arguments is making sure that the
values used when calling the keyword match the correct arguments. This
is a problem especially if there are multiple arguments and characters
separating them may also appear in the given values. For example,
Select Los Angeles Lakers in the following example matches
Select ${city} ${team} so that ${city} contains Los and
${team} contains Angeles Lakers:
An easy solution to this problem is surrounding arguments with double quotes or other characters not used in the actual values. This fixed example works so that cities and teams match correctly:
This approach is not enough to resolve all conflicts, but it helps in common cases and is generally recommended. Another benefit is that it makes arguments stand out from rest of the keyword.
Prior to Robot Framework 7.1, embedded arguments starting the keyword name also
matched possible given/when/then/and/but prefixes typically used in Behavior
Driven Development (BDD). For example, ${name} goes home matched
Given Janne goes home so that ${name} got value Given Janne.
Nowadays the prefix is ignored and ${name} will be Janne as expected.
If older Robot Framework versions need to be supported, it is easiest to quote
the argument like in "${name}" goes home to get consistent behavior.
An alternative solution for limiting what values arguments match is using custom regular expressions.
Resolving conflicts¶
When using embedded arguments, it is pretty common that there are multiple
keyword implementations that match the keyword that is used. For example,
Execute "ls" with "lf" in the example below matches both of the keywords.
It matching Execute "${cmd}" with "${opts}" is pretty obvious and what
we want, but it also matches Execute "${cmd}" so that ${cmd} matches
ls" with "-lh.
When this kind of conflicts occur, Robot Framework tries to automatically select the best match and use that. In the above example, Execute "${cmd}" with "${opts}" is considered a better match than the more generic Execute "${cmd}" and running the example thus succeeds without conflicts.
It is not always possible to find a single match that is better than others. For example, the second test below fails because Robot Framework matches both of the keywords equally well. This kind of conflicts need to be resolved manually either by renaming keywords or by using custom regular expressions.
Keywords that accept only "normal" arguments or no arguments at all are considered to match better than keywords accepting embedded arguments. For example, if the following keyword is added to the above example, Robot Framework used by the latter test matches it and the test succeeds:
Before looking which match is best, Robot Framework checks are some of the matching keywords implemented in the same file as the caller keyword. If there are such keywords, they are given precedence over other keywords. Alternatively, library search order can be used to control the order in which Robot Framework looks for keywords in resources and libraries.
Note
Automatically resolving conflicts if multiple keywords with embedded arguments match is a new feature in Robot Framework 6.0. With older versions custom regular expressions explained below can be used instead.
Using custom regular expressions¶
When keywords with embedded arguments are called, the values are matched
internally using regular expressions (regexps for short). The default
logic goes so that every argument in the name is replaced with a pattern .*?
that matches any string and tries to match as little as possible. This logic works
fairly well normally, but as discussed above, sometimes keywords
match wrong values and sometimes there are conflicts that cannot
be resolved . A solution in these cases is specifying a custom regular
expression that makes sure that the keyword matches only what it should in that
particular context. To be able to use this feature, and to fully
understand the examples in this section, you need to understand at
least the basics of the regular expression syntax.
A custom embedded argument regular expression is defined after the
base name of the argument so that the argument and the regexp are
separated with a colon. For example, an argument that should match
only numbers can be defined like ${arg:\d+}.
If needed, custom patterns can be prefixed with inline flags such as
(?i) for case-insensitivity.
Using custom regular expressions is illustrated by the following examples.
The first one shows how the earlier problem with Select ${city} ${team}
not matching Select Los Angeles Lakers properly can be resolved without
quoting by implementing the keyword so that ${team} can only contain non-whitespace
characters.
Note
Support for inline flags is new in Robot Framework 7.2.
Supported regular expression syntax¶
Being implemented with Python, Robot Framework naturally uses Python's
re module that has pretty standard regular expressions syntax.
This syntax is otherwise fully supported with embedded arguments, but
regexp extensions in format (?...) cannot be used. If the regular
expression syntax is invalid, creating the keyword fails with an error
visible in test execution errors.
Escaping special characters¶
Regular expressions use the backslash character (\) heavily both
to form special sequences (e.g. \d) and to escape characters that have
a special meaning in regexps (e.g. \$). Typically in Robot Framework data
backslash characters need to be escaped with another backslash, but
that is not required in this context. If there is a need to have a literal
backslash in the pattern, then the backslash must be escaped like
${path:c:\\temp\\.*}.
Possible lone opening and closing curly braces in the pattern must be escaped
like ${open:\{} and ${close:\}} or otherwise Robot Framework is not able
to parse the variable syntax correctly. If there are matching braces like in
${digits:\d{2}}, escaping is not needed.
Note
Prior to Robot Framework 3.2, it was mandatory to escape all
closing curly braces in the pattern like ${digits:\d{2\}}.
This syntax is unfortunately not supported by Robot Framework 3.2
or newer and keywords using it must be updated when upgrading.
Note
Prior to Robot Framework 6.0, using literal backslashes in the pattern
required double escaping them like ${path:c:\\\\temp\\\\.*}.
Patterns using literal backslashes need to be updated when upgrading.
Using variables with custom embedded argument regular expressions¶
When using embedded arguments with custom regular expressions, specifying
values using variables works only if variables match the whole embedded
argument, not if there is any additional content with the variable.
For example, the first test below succeeds because the variable ${DATE}
is used on its own, but the last test fails because ${YEAR}-${MONTH}-${DAY}
is not a single variable.
Another limitation of using variables is that their actual values are not matched against custom regular expressions. As the result keywords may be called with values that their custom regexps would not allow. This behavior is deprecated starting from Robot Framework 6.0 and values will be validated in the future. For more information see issue #4462.
Argument conversion with embedded arguments¶
User keywords accepting embedded arguments support argument conversion with type
syntax ${name: type} similarly as normal user keywords. If a custom pattern
is needed, it can be separated with an additional colon like ${name: type:pattern}.
Because the type separator is a colon followed by a space (e.g. ${arg: int})
and the pattern separator is just a colon (e.g. ${arg:\d+}), there typically
are no conflicts when using only a type or only a pattern. The only exception
is using a pattern starting with a space, but in that case the space can be
escaped like ${arg:\ abc} or a type added like ${arg: str: abc}.
Note
Argument conversion with user keywords is new in Robot Framework 7.3.
Behavior-driven development example¶
A big benefit of having arguments as part of the keyword name is that it makes it easier to use higher-level sentence-like keywords when using the behavior-driven style to write tests. As the example below shows, this support is typically used in combination with the possibility to omit Given, When and Then prefixes in keyword definitions:
Note
Embedded arguments feature in Robot Framework is inspired by how step definitions are created in the popular BDD tool Cucumber.
User keyword return values¶
Similarly as library keywords, also user keywords can return values. When using Robot Framework 5.0 or newer, the recommended approach is using the native RETURN statement. The old [Return] setting was deprecated in Robot Framework 7.0 and also BuiltIn keywords Return From Keyword and Return From Keyword If are considered deprecated.
Regardless how values are returned, they can be assigned to variables in test cases and in other user keywords.
Using RETURN statement¶
The recommended approach to return values is using the RETURN statement.
It accepts optional return values and can be used with IF and inline IF
structures. Its usage is easiest explained with examples:
If you want to test the above examples yourself, you can use them with these test cases:
Note
RETURN is new in Robot Framework 5.0. Use approaches explained
below if you need to support older versions.
Using [Return] setting¶
The [Return] setting defines what the keyword should return after it has been executed. Although it is recommended to have it at the end of keyword where it logically belongs, its position does not affect how it is used.
An inherent limitation of the [Return] setting is that cannot be used
conditionally. Thus only the first two earlier RETURN statement examples
can be created using it.
Note
The [Return] setting was deprecated in Robot Framework 7.0
and the RETURN statement should be used instead. If there is a need
to support older Robot Framework versions that do not support RETURN,
it is possible to use the special keywords discussed in the next section.
Using special keywords to return¶
BuiltIn keywords Return From Keyword and Return From Keyword If
allow returning from a user keyword conditionally in the middle of the keyword.
Both of them also accept optional return values that are handled exactly like
with the RETURN statement and the [Return] setting discussed above.
The introduction of the RETURN statement makes these keywords redundant.
Examples below contain same keywords as earlier RETURN examples but these
ones are more verbose:
Note
These keywords are effectively deprecated and the RETURN statement should be
used unless there is a need to support also older versions than Robot Framework
5.0. There is no visible deprecation warning when using these keywords yet, but
they will be loudly deprecated and eventually removed in the future.
User keyword setup and teardown¶
A user keyword can have a setup and a teardown similarly as tests. They are specified using [Setup] and [Teardown] settings, respectively, directly to the keyword having them. Unlike with tests, it is not possible to specify a common setup or teardown to all keywords in a certain file.
A setup and a teardown are always a single keyword, but they can themselves be
user keywords executing multiple keywords internally. It is possible to specify
them as variables, and using a special NONE value (case-insensitive) is
the same as not having a setup or a teardown at all.
User keyword setup is not much different to the first keyword inside the created user keyword. The only functional difference is that a setup can be specified as a variable, but it can also be useful to be able to explicitly mark a keyword to be a setup.
User keyword teardowns are, exactly as test teardowns, executed also if the user keyword fails. They are thus very useful when needing to do something at the end of the keyword regardless of its status. To ensure that all cleanup activities are done, the continue on failure mode is enabled by default with user keyword teardowns the same way as with test teardowns.
Note
User keyword setups are new in Robot Framework 7.0.
Private user keywords¶
User keywords can be tagged with a special robot:private tag to indicate
that they should only be used in the file where they are created:
Using the robot:private tag does not outright prevent using the keyword
outside the file where it is created, but such usages will cause a warning.
If there is both a public and a private keyword with the same name,
the public one will be used but also this situation causes a warning.
Private keywords are included in spec files created by Libdoc but not in its HTML output files.
Note
Private user keywords are new in Robot Framework 6.0.
Recursion¶
User keywords can call themselves either directly or indirectly. This kind of recursive usage is fine as long as the recursion ends, typically based on some condition, before the recursion limit is exceeded. The limit exists because otherwise infinite recursion would crash the execution.
Robot Framework's recursion detection works so, that it checks is the current recursion level close to the recursion limit of the underlying Python process. If it is close enough, no more new started keywords or control structures are allowed and execution fails.
Python's default recursion limit is 1000 stack frames, which in practice means that it is possible to start approximately 140 keywords or control structures. If that is not enough, Python's recursion limit can be raised using the sys.setrecursionlimit() function. As the documentation of the function explains, this should be done with care, because a too-high level can lead to a crash.
Note
Prior to Robot Framework 7.2, the recursion limit was hard-coded to 100 started keywords or control structures.