Library version: | 2.9.2 |
---|---|
Library scope: | global |
Named arguments: | supported |
Robot Framework test library for running processes.
This library utilizes Python's subprocess module and its Popen class.
The library has following main usages:
This library is new in Robot Framework 2.8.
Both Run Process and Start Process accept the command to execute and all arguments passed to the command as separate arguments. This makes usage convenient and also allows these keywords to automatically escape possible spaces and other special characters in commands and arguments. Notice that if a command accepts options that themselves accept values, these options and their values must be given as separate arguments.
When running processes in shell, it is also possible to give the whole command to execute as a single string. The command can then contain multiple commands to be run together. When using this approach, the caller is responsible on escaping.
Examples:
Run Process | ${tools}${/}prog.py | argument | second arg with spaces | ||
Run Process | java | -jar | ${jars}${/}example.jar | --option | value |
Run Process | prog.py "one arg" && tool.sh | shell=yes | cwd=${tools} |
Starting from Robot Framework 2.8.6, possible non-string arguments are converted to strings automatically.
Run Process and Start Process keywords can be configured using optional **configuration
keyword arguments. Configuration arguments must be given after other arguments passed to these keywords and must use syntax like name=value
. Available configuration arguments are listed below and discussed further in sections afterwards.
Name | Explanation |
---|---|
shell | Specifies whether to run the command in shell or not. |
cwd | Specifies the working directory. |
env | Specifies environment variables given to the process. |
env:<name> | Overrides the named environment variable(s) only. |
stdout | Path of a file where to write standard output. |
stderr | Path of a file where to write standard error. |
alias | Alias given to the process. |
Note that because **configuration
is passed using name=value
syntax, possible equal signs in other arguments passed to Run Process and Start Process must be escaped with a backslash like name\=value
. See Run Process for an example.
The shell
argument specifies whether to run the process in a shell or not. By default shell is not used, which means that shell specific commands, like copy
and dir
on Windows, are not available. You can, however, run shell scripts and batch files without using a shell.
Giving the shell
argument any non-false value, such as shell=True
, changes the program to be executed in a shell. It allows using the shell capabilities, but can also make the process invocation operating system dependent. Having a shell between the actually started process and this library can also interfere communication with the process such as stopping it and reading its outputs. Because of these problems, it is recommended to use the shell only when absolutely necessary.
When using a shell it is possible to give the whole command to execute as a single string. See Specifying command and arguments section for examples and more details in general.
By default the child process will be executed in the same directory as the parent process, the process running tests, is executed. This can be changed by giving an alternative location using the cwd
argument. Forward slashes in the given path are automatically converted to backslashes on Windows.
Standard output and error streams, when redirected to files, are also relative to the current working directory possibly set using the cwd
argument.
Example:
Run Process | prog.exe | cwd=${ROOT}/directory | stdout=stdout.txt |
By default the child process will get a copy of the parent process's environment variables. The env
argument can be used to give the child a custom environment as a Python dictionary. If there is a need to specify only certain environment variable, it is possible to use the env:<name>=<value>
format to set or override only that named variables. It is also possible to use these two approaches together.
Examples:
Run Process | program | env=${environ} | |
Run Process | program | env:http_proxy=10.144.1.10:8080 | env:PATH=%{PATH}${:}${PROGDIR} |
Run Process | program | env=${environ} | env:EXTRA=value |
By default processes are run so that their standard output and standard error streams are kept in the memory. This works fine normally, but if there is a lot of output, the output buffers may get full and the program can hang. Additionally on Jython, everything written to these in-memory buffers can be lost if the process is terminated.
To avoid the above mentioned problems, it is possible to use stdout
and stderr
arguments to specify files on the file system where to redirect the outputs. This can also be useful if other processes or other keywords need to read or manipulate the outputs somehow.
Given stdout
and stderr
paths are relative to the current working directory. Forward slashes in the given paths are automatically converted to backslashes on Windows.
As a special feature, it is possible to redirect the standard error to the standard output by using stderr=STDOUT
.
Regardless are outputs redirected to files or not, they are accessible through the result object returned when the process ends.
Examples:
${result} = | Run Process | program | stdout=${TEMPDIR}/stdout.txt | stderr=${TEMPDIR}/stderr.txt |
Log Many | stdout: ${result.stdout} | stderr: ${result.stderr} | ||
${result} = | Run Process | program | stderr=STDOUT | |
Log | all output: ${result.stdout} |
Note that the created output files are not automatically removed after the test run. The user is responsible to remove them if needed.
A custom name given to the process that can be used when selecting the active process.
Examples:
Start Process | program | alias=example | ||
Run Process | python | -c | print 'hello' | alias=hello |
The test library keeps record which of the started processes is currently active. By default it is latest process started with Start Process, but Switch Process can be used to select a different one. Using Run Process does not affect the active process.
The keywords that operate on started processes will use the active process by default, but it is possible to explicitly select a different process using the handle
argument. The handle can be the identifier returned by Start Process or an alias
explicitly given to Start Process or Run Process.
Run Process, Wait For Process and Terminate Process keywords return a result object that contains information about the process execution as its attributes. The same result object, or some of its attributes, can also be get using Get Process Result keyword. Attributes available in the object are documented in the table below.
Attribute | Explanation |
---|---|
rc | Return code of the process as an integer. |
stdout | Contents of the standard output stream. |
stderr | Contents of the standard error stream. |
stdout_path | Path where stdout was redirected or None if not redirected. |
stderr_path | Path where stderr was redirected or None if not redirected. |
Example:
${result} = | Run Process | program |
Should Be Equal As Integers | ${result.rc} | 0 |
Should Match | ${result.stdout} | Some t?xt* |
Should Be Empty | ${result.stderr} | |
${stdout} = | Get File | ${result.stdout_path} |
Should Be Equal | ${stdout} | ${result.stdout} |
File Should Be Empty | ${result.stderr_path} |
Some keywords accept arguments that are handled as Boolean values true or false. If such an argument is given as a string, it is considered false if it is either empty or case-insensitively equal to false
or no
. Other strings are considered true regardless their value, and other argument types are tested using same rules as in Python.
True examples:
Terminate Process | kill=True | # Strings are generally true. |
Terminate Process | kill=yes | # Same as the above. |
Terminate Process | kill=${TRUE} | # Python True is true. |
Terminate Process | kill=${42} | # Numbers other than 0 are true. |
False examples:
Terminate Process | kill=False | # String false is false. |
Terminate Process | kill=no | # Also string no is false. |
Terminate Process | kill=${EMPTY} | # Empty string is false. |
Terminate Process | kill=${FALSE} | # Python False is false. |
Note that prior to Robot Framework 2.8 all non-empty strings, including false
, were considered true. Additionally, no
is considered false only in Robot Framework 2.9 and newer.
The OperatingSystem library also contains keywords for running processes. They are not as flexible as the keywords provided by this library, and thus not recommended to be used anymore. They may eventually even be deprecated.
There is a name collision because both of these libraries have Start Process and Switch Process keywords. This is handled so that if both libraries are imported, the keywords in the Process library are used by default. If there is a need to use the OperatingSystem variants, it is possible to use OperatingSystem.Start Process syntax or use the BuiltIn keyword Set Library Search Order to change the priority.
Other keywords in the OperatingSystem library can be used freely with keywords in the Process library.
*** Settings *** Library Process Suite Teardown Terminate All Processes kill=True *** Test Cases *** Example Start Process program arg1 arg2 alias=First ${handle} = Start Process command.sh arg | command2.sh shell=True cwd=/path ${result} = Run Process ${CURDIR}/script.py Should Not Contain ${result.stdout} FAIL Terminate Process ${handle} ${result} = Wait For Process First Should Be Equal As Integers ${result.rc} 0
Keyword | Arguments | Documentation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Get Process Id | handle=None | Returns the process ID (pid) of the process as an integer. If Notice that the pid is not the same as the handle returned by Start Process that is used internally by this library. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Get Process Object | handle=None | Return the underlying If | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Get Process Result | handle=None, rc=False, stdout=False, stderr=False, stdout_path=False, stderr_path=False | Returns the specified result object or some of its attributes. The given If no other arguments than the optional Examples:
Although getting results of a previously executed process can be handy in general, the main use case for this keyword is returning results over the remote library interface. The remote interface does not support returning the whole result object, but individual attributes can be returned without problems. New in Robot Framework 2.8.2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Is Process Running | handle=None | Checks is the process running or not. If Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Join Command Line | *args | Joins arguments into one command line string. In resulting command line string arguments are delimited with a space, arguments containing spaces are surrounded with quotes, and possible quotes are escaped with a backslash. If this keyword is given only one argument and that is a list like object, then the values of that list are joined instead. Example:
New in Robot Framework 2.9.2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Process Should Be Running | handle=None, error_message=Process is not running. | Verifies that the process is running. If Fails if the process has stopped. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Process Should Be Stopped | handle=None, error_message=Process is running. | Verifies that the process is not running. If Fails if the process is still running. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Run Process | command, *arguments, **configuration | Runs a process and waits for it to complete.
Returns a result object containing information about the execution. Note that possible equal signs in Examples:
This keyword does not change the active process.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Send Signal To Process | signal, handle=None, group=False | Sends the given If Signal can be specified either as an integer as a signal name. In the latter case it is possible to give the name both with or without
This keyword is only supported on Unix-like machines, not on Windows. What signals are supported depends on the system. For a list of existing signals on your system, see the Unix man pages related to signal handling (typically By default sends the signal only to the parent process, not to possible child processes started by it. Notice that when running processes in shell, the shell is the parent process and it depends on the system does the shell propagate the signal to the actual started process. To send the signal to the whole process group, New in Robot Framework 2.8.2. Support for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Split Command Line | args, escaping=False | Splits command line string into a list of arguments. String is split from spaces, but argument surrounded in quotes may contain spaces in them. If Examples:
New in Robot Framework 2.9.2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Start Process | command, *arguments, **configuration | Starts a new process on background. See Specifying command and arguments and Process configuration for more information about the arguments, and Run Process keyword for related examples. Makes the started process new active process. Returns an identifier that can be used as a handle to active the started process if needed. Starting from Robot Framework 2.8.5, processes are started so that they create a new process group. This allows sending signals to and terminating also possible child processes. This is not supported by Jython in general nor by Python versions prior to 2.7 on Windows. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Switch Process | handle | Makes the specified process the current active process. The handle can be an identifier returned by Start Process or the Example:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Terminate All Processes | kill=False | Terminates all still running processes started by this library. This keyword can be used in suite teardown or elsewhere to make sure that all processes are stopped, By default tries to terminate processes gracefully, but can be configured to forcefully kill them immediately. See Terminate Process that this keyword uses internally for more details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Terminate Process | handle=None, kill=False | Stops the process gracefully or forcefully. If By default first tries to stop the process gracefully. If the process does not stop in 30 seconds, or Waits for the process to stop after terminating it. Returns a result object containing information about the execution similarly as Wait For Process. On Unix-like machines graceful termination is done using On Windows graceful termination is done using Examples:
Limitations:
Automatically killing the process if termination fails as well as returning a result object are new features in Robot Framework 2.8.2. Terminating also possible child processes, including using | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Wait For Process | handle=None, timeout=None, on_timeout=continue | Waits for the process to complete or to reach the given timeout. The process to wait for must have been started earlier with Start Process. If
See Terminate Process keyword for more details how processes are terminated and killed. If the process ends before the timeout or it is terminated or killed, this keyword returns a result object containing information about the execution. If the process is left running, Python Examples:
|