robot.api package¶
robot.api package exposes the public APIs of Robot Framework.
Unless stated otherwise, the APIs exposed in this package are considered stable, and thus safe to use when building external tools on top of Robot Framework.
Currently exposed APIs are:
loggermodule for test libraries’ logging purposes.decomodule with decorators test libraries can utilize.TestCaseFile,TestDataDirectory, andResourceFileclasses for parsing test data files and directories. In addition, a convenience factory methodTestData()creates eitherTestCaseFileorTestDataDirectoryobjects based on the input.TestSuiteclass for creating executable test suites programmatically andTestSuiteBuilderclass for creating such suites based on existing test data on the file system.SuiteVisitorabstract class for processing testdata before execution. This can be used as a base for implementing a pre-run modifier that is taken into use with--prerunmodifiercommandline option.ExecutionResult()factory method for reading execution results from XML output files andResultVisitorabstract class to ease further processing the results.ResultVisitorcan also be used as a base for pre-Rebot modifier that is taken into use with--prerebotmodifiercommandline option.ResultWriterclass for writing reports, logs, XML outputs, and XUnit files. Can write results based on XML outputs on the file system, as well as based on the result objects returned by theExecutionResult()or an executedTestSuite.
All of the above names can be imported like:
from robot.api import ApiName
See documentations of the individual APIs for more details.
Tip
APIs related to the command line entry points are exposed directly
via the robot root package.
Submodules¶
robot.api.deco module¶
-
robot.api.deco.keyword(name=None, tags=(), types=())[source]¶ Decorator to set custom name, tags and argument types to keywords.
This decorator creates
robot_name,robot_tagsandrobot_typesattributes on the decorated keyword method or function based on the provided arguments. Robot Framework checks them to determine the keyword’s name, tags, and argument types, respectively.Name must be given as a string, tags as a list of strings, and types either as a dictionary mapping argument names to types or as a list (or tuple) of types mapped to arguments based on position. It is OK to specify types only to some arguments, and setting
typestoNonedisables type conversion altogether.Examples:
@keyword(name='Login Via User Panel') def login(username, password): # ... @keyword(name='Logout Via User Panel', tags=['example', 'tags']) def logout(): # ... @keyword(types={'length': int, 'case_insensitive': bool}) def types_as_dict(length, case_insensitive=False): # ... @keyword(types=[int, bool]) def types_as_list(length, case_insensitive=False): # ... @keyword(types=None]) def no_conversion(length, case_insensitive=False): # ...
If
nameis not given, the actual name of the keyword will not be affected, but therobot_nameattribute will still be created. This can be useful for marking methods as keywords in a dynamic library. In this usage it is possible to also omit parenthesis when using the decorator:@keyword def func(): # ...
robot.api.logger module¶
Public logging API for test libraries.
This module provides a public API for writing messages to the log file and the console. Test libraries can use this API like:
logger.info('My message')
instead of logging through the standard output like:
print '*INFO* My message'
In addition to a programmatic interface being cleaner to use, this API has a benefit that the log messages have accurate timestamps.
If the logging methods are used when Robot Framework is not running,
the messages are redirected to the standard Python logging module
using logger named RobotFramework.
Log levels¶
It is possible to log messages using levels TRACE, DEBUG, INFO,
WARN and ERROR either using the write() function or, more
commonly, with the log level specific trace(), debug(),
info(), warn(), error() functions. The support for the
error level and function is new in RF 2.9.
By default the trace and debug messages are not logged but that can be
changed with the --loglevel command line option. Warnings and errors are
automatically written also to the console and to the Test Execution Errors
section in the log file.
Logging HTML¶
All methods that are used for writing messages to the log file have an
optional html argument. If a message to be logged is supposed to be
shown as HTML, this argument should be set to True. Alternatively,
write() accepts a pseudo log level HTML.
Example¶
from robot.api import logger
def my_keyword(arg):
logger.debug('Got argument %s.' % arg)
do_something()
logger.info('<i>This</i> is a boring example.', html=True)
-
robot.api.logger.write(msg, level='INFO', html=False)[source]¶ Writes the message to the log file using the given level.
Valid log levels are
TRACE,DEBUG,INFO(default since RF 2.9.1),WARN, andERROR(new in RF 2.9). Additionally it is possible to useHTMLpseudo log level that logs the message as HTML using theINFOlevel.Instead of using this method, it is generally better to use the level specific methods such as
infoanddebugthat have separatehtmlargument to control the message format.
-
robot.api.logger.trace(msg, html=False)[source]¶ Writes the message to the log file using the
TRACElevel.
-
robot.api.logger.debug(msg, html=False)[source]¶ Writes the message to the log file using the
DEBUGlevel.
-
robot.api.logger.info(msg, html=False, also_console=False)[source]¶ Writes the message to the log file using the
INFOlevel.If
also_consoleargument is set toTrue, the message is written both to the log file and to the console.
-
robot.api.logger.warn(msg, html=False)[source]¶ Writes the message to the log file using the
WARNlevel.
-
robot.api.logger.error(msg, html=False)[source]¶ Writes the message to the log file using the
ERRORlevel.New in Robot Framework 2.9.
-
robot.api.logger.console(msg, newline=True, stream='stdout')[source]¶ Writes the message to the console.
If the
newlineargument isTrue, a newline character is automatically added to the message.By default the message is written to the standard output stream. Using the standard error stream is possibly by giving the
streamargument value'stderr'.