RobotFramework之Process

Process

Library version: 3.0.4
Library scope: global
Named arguments: supported

Introduction

Robot Framework test library for running processes.html

This library utilizes Python's subprocess module and its Popen class.java

The library has following main usages:python

This library is new in Robot Framework 2.8.shell

Table of contents

Specifying command and arguments

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.api

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.app

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.less

Process configuration

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.ide

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.
output_encoding Encoding to use when reading command outputs.
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.函數

Running processes in shell

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.

Current working directory

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

Environment variables

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

Standard output and error streams

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. Commands are expected to write outputs using the console encoding, but output encoding can be configured using the output_encoding argument if needed.

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.

Output encoding

Executed commands are, by default, expected to write outputs to the standard output and error streams using the encoding used by the system console. If the command uses some other encoding, that can be configured using the output_encoding argument. This is especially useful on Windows where the console uses a different encoding than rest of the system, and many commands use the general system encoding instead of the console encoding.

The value used with the output_encoding argument must be a valid encoding and must match the encoding actually used by the command. As a convenience, it is possible to use strings CONSOLE and SYSTEM to specify that the console or system encoding is used, respectively. If produced outputs use different encoding then configured, values got through the result object will be invalid.

Examples:

Start Process program output_encoding=UTF-8  
Run Process program stdout=${path} output_encoding=SYSTEM

The support to set output encoding is new in Robot Framework 3.0.

Alias

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

Active process

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.

Result object

Run ProcessWait 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}  

Boolean arguments

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 an empty string or case-insensitively equal to falsenoneor no. Other strings are considered true regardless their value, and other argument types are tested using the 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.

Prior to Robot Framework 2.9, all non-empty strings, including false and no, were considered to be true. Considering none false is new in Robot Framework 3.0.3.

Example

*** 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

Shortcuts

Get Process Id · Get Process Object · Get Process Result · Is Process Running · Join Command Line · Process Should Be Running · Process Should Be Stopped · Run Process · Send Signal To Process · Split Command Line · Start Process ·Switch Process · Terminate All Processes · Terminate Process · Wait For Process

Keywords

Keyword Arguments Documentation
Get Process Id handle=None

Returns the process ID (pid) of the process as an integer.

If handle is not given, uses the current active process.

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 subprocess.Popen object.

If handle is not given, uses the current active process.

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 handle specifies the process whose results should be returned. If no handle is given, results of the current active process are returned. In either case, the process must have been finishes before this keyword can be used. In practice this means that processes started with Start Process must be finished either with Wait For Process or Terminate Process before using this keyword.

If no other arguments than the optional handle are given, a whole result object is returned. If one or more of the other arguments are given any true value, only the specified attributes of the result object are returned. These attributes are always returned in the same order as arguments are specified in the keyword signature. See Boolean arguments section for more details about true and false values.

Examples:

Run Process python -c print 'Hello, world!' alias=myproc  
# Get result object          
${result} = Get Process Result myproc      
Should Be Equal ${result.rc} ${0}      
Should Be Equal ${result.stdout} Hello, world!      
Should Be Empty ${result.stderr}        
# Get one attribute          
${stdout} = Get Process Result myproc stdout=true    
Should Be Equal ${stdout} Hello, world!      
# Multiple attributes          
${stdout} ${stderr} = Get Process Result myproc stdout=yes stderr=yes
Should Be Equal ${stdout} Hello, world!      
Should Be Empty ${stderr}        

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 handle is not given, uses the current active process.

Returns True if the process is still running and False otherwise.

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:

${cmd} = Join Command Line --option value with spaces
Should Be Equal ${cmd} --option "value with spaces"  

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 handle is not given, uses the current active process.

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 handle is not given, uses the current active process.

Fails if the process is still running.

Run Process command, *arguments,**configuration

Runs a process and waits for it to complete.

command and *arguments specify the command to execute and arguments passed to it. See Specifying command and arguments for more details.

**configuration contains additional configuration related to starting processes and waiting for them to finish. See Process configuration for more details about configuration related to starting processes. Configuration related to waiting for processes consists of timeout and on_timeout arguments that have same semantics as with Wait For Process keyword. By default there is no timeout, and if timeout is defined the default action on timeout is terminate.

Returns a result object containing information about the execution.

Note that possible equal signs in *arguments must be escaped with a backslash (e.g. name\=value) to avoid them to be passed in as **configuration.

Examples:

${result} = Run Process python -c print 'Hello, world!'
Should Be Equal ${result.stdout} Hello, world!    
${result} = Run Process ${command} stderr=STDOUT timeout=10s
${result} = Run Process ${command} timeout=1min on_timeout=continue
${result} = Run Process java -Dname\=value Example shell=True cwd=${EXAMPLE}

This keyword does not change the active process.

timeout and on_timeout arguments are new in Robot Framework 2.8.4.

Send Signal To Process signal, handle=None,group=False

Sends the given signal to the specified process.

If handle is not given, uses the current active process.

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 SIG prefix, but names are case-sensitive. For example, all the examples below send signal INT (2):

Send Signal To Process 2   # Send to active process
Send Signal To Process INT    
Send Signal To Process SIGINT myproc # Send to named process

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 man signal or man 7 signal).

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, group argument can be set to any true value (see Boolean arguments). This is not supported by Jython, however.

New in Robot Framework 2.8.2. Support for group argument is new in Robot Framework 2.8.5.

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 escaping is given a true value, then backslash is treated as an escape character. It can escape unquoted spaces, quotes inside quotes, and so on, but it also requires using double backslashes when using Windows paths.

Examples:

@{cmd} = Split Command Line --option "value with spaces"
Should Be True $cmd == ['--option', 'value with spaces']  

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 activate 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 alias given to it explicitly.

Example:

Start Process prog1 alias=process1
Start Process prog2 alias=process2
# currently active process is process2    
Switch Process process1  
# now active process is process1    
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 handle is not given, uses the current active process.

By default first tries to stop the process gracefully. If the process does not stop in 30 seconds, or kill argument is given a true value, (see Boolean arguments) kills the process forcefully. Stops also all the child processes of the originally started process.

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 TERM (15) signal and killing using KILL (9). Use Send Signal To Process instead if you just want to send either of these signals without waiting for the process to stop.

On Windows graceful termination is done using CTRL_BREAK_EVENT event and killing using Win32 API function TerminateProcess().

Examples:

${result} = Terminate Process    
Should Be Equal As Integers ${result.rc} -15 # On Unixes
Terminate Process myproc kill=true  

Limitations:

  • Graceful termination is not supported on Windows by Jython nor by Python versions prior to 2.7. Process is killed instead.
  • Stopping the whole process group is not supported by Jython at all nor by Python versions prior to 2.7 on Windows.
  • On Windows forceful kill only stops the main process, not possible child processes.

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 CTRL_BREAK_EVENT on Windows, is new in Robot Framework 2.8.5.

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 handle is not given, uses the current active process.

timeout defines the maximum time to wait for the process. It can be given in various time formats supported by Robot Framework, for example, 4242 s, or 1 minute 30 seconds.

on_timeout defines what to do if the timeout occurs. Possible values and corresponding actions are explained in the table below. Notice that reaching the timeout never fails the test.

Value Action
continue The process is left running (default).
terminate The process is gracefully terminated.
kill The process is forcefully stopped.

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 None is returned instead.

Examples:

# Process ends cleanly      
${result} = Wait For Process example  
Process Should Be Stopped example    
Should Be Equal As Integers ${result.rc} 0  
# Process does not end      
${result} = Wait For Process timeout=42 secs  
Process Should Be Running      
Should Be Equal ${result} ${NONE}  
# Kill non-ending process      
${result} = Wait For Process timeout=1min 30s on_timeout=kill
Process Should Be Stopped      
Should Be Equal As Integers ${result.rc} -9  

timeout and on_timeout are new in Robot Framework 2.8.2.

Altogether 15 keywords. 
Generated by Libdoc on 2018-04-25 23:41:29.

處理

圖書館版本: 3.0.4
圖書館範圍: 全球
命名參數: 支持的

介紹

用於運行進程的Robot Framework測試庫。

該庫使用Python的子進程模塊及其Popen類。

該圖書館有如下主要用途:

這個庫是Robot Framework 2.8中的新增功能。

目錄

指定命令和參數

不管運行過程啓動過程接受執行命令,並傳遞給命令做爲獨立參數,全部參數。這使得使用方便,而且還容許這些關鍵字自動轉義命令和參數中的可能空格和其餘特殊字符。請注意,若是命令接受本身接受值的選項,則必須將這些選項及其值做爲單獨的參數給出。

運行在殼進程,它也能夠給整個命令來執行做爲單個字符串。而後,該命令能夠包含多個要一塊兒運行的命令。使用此方法時,調用者負責轉義。

例子:

運行流程 $ {工具} $ {/} prog.py 論據 第二個arg有空格    
運行流程 java的 -罐 $ {罐} $ {/} example.jar - 選項
運行流程 prog.py「one arg」&& tool.sh 殼= YES CWD = $ {}工具    

從Robot Framework 2.8.6開始,可能的非字符串參數會自動轉換爲字符串。

流程配置

可使用可選的關鍵字參數配置Run ProcessStart Process關鍵字**configuration。必須在傳遞給這些關鍵字的其餘參數以後給出配置參數,而且必須使用相似語法name=value。下面列出了可用的配置參數,而後在後面的章節中進一步討論。

名稱 說明
貝殼 指定是否在shell中運行命令。
CWD 指定工做目錄。
ENV 指定爲進程指定的環境變量。
ENV:<名稱> 僅覆蓋指定的環境變量。
標準輸出 文件的路徑寫入標準輸出的位置。
標準錯誤 文件的路徑在哪裏寫標準錯誤。
output_encoding 在讀取命令輸出時使用的編碼。
別號 別名給予該過程。

請注意,由於**configuration使用name=value語法傳遞,因此傳遞給Run ProcessStart Process的其餘參數中的可能等號必須使用反斜槓轉義name\=value。有關示例,請參閱運行過程

在shell中運行進程

shell參數指定是否在外殼或沒法運行的過程。默認狀況下,外殼沒有使用,這意味着外殼特定命令,就像copydir在Windows,不可用。可是,您能夠在不使用shell的狀況下運行shell腳本和批處理文件。

shell參數賦予任何非假值,例如shell=True,更改要在shell中執行的程序。它容許使用shell功能,但也可使進程調用操做系統依賴。在實際啓動的進程和此庫之間具備shell也可能干擾與進程的通訊,例如中止它並讀取其輸出。因爲這些問題,建議僅在絕對必要時才使用shell。

使用shell時,能夠將整個命令做爲單個字符串執行。有關示例和更多詳細信息,請參閱指定命令和參數部分。

當前的工做目錄

默認狀況下,子進程將在與父進程相同的目錄中執行,執行運行測試的進程。這能夠經過使用cwd參數提供替代位置來更改。在給定路徑中的正斜槓自動轉換爲Windows上的反斜槓。

重定向到文件時,標準輸出和錯誤流也與可能使用cwd參數設置的當前工做目錄相關。

例:

運行流程 prog.exe CWD = $ {ROOT} /目錄 標準輸出= stdout.txt

環境變量

默認狀況下,子進程將獲取父進程的環境變量的副本。該env參數可用於爲子項提供自定義環境做爲Python字典。若是隻須要指定某個環境變量,則可使用該env:<name>=<value>格式來設置或僅覆蓋該命名變量。也能夠將這兩種方法結合使用。

例子:

運行流程 程序 ENV = $ {} ENVIRON  
運行流程 程序 ENV:HTTP_PROXY = 10.144.1.10:8080 ENV:PATH =%{PATH} $ {:} $ {} PROGDIR
運行流程 程序 ENV = $ {} ENVIRON ENV:EXTRA =值

標準輸出和錯誤流

默認狀況下,運行進程以使其標準輸出和標準錯誤流保留在內存中。這一般工做正常,但若是有不少輸出,輸出緩衝區可能會滿,程序可能會掛起。此外,在Jython上,若是進程終止,寫入這些內存緩衝區的全部內容均可能會丟失。

爲了不上述問題,可使用stdoutstderr參數來指定文件系統上重定向輸出的文件。若是其餘進程或其餘關鍵字須要以某種方式讀取或操做輸出,這也頗有用。

給定stdoutstderr路徑相對於當前工做目錄。在給定路徑中的正斜槓自動轉換爲Windows上的反斜槓。

做爲一項特殊功能,可使用標準錯誤將標準錯誤重定向到標準輸出stderr=STDOUT

不管是否重定向到文件的輸出,均可以經過進程結束時返回的結果對象訪問它們。命令指望使用控制檯編碼寫入輸出,可是若是須要,可使用參數配置輸出編碼output_encoding

例子:

$ {result} = 運行流程 程序 標準輸出= $ {TEMPDIR} /stdout.txt 標準錯誤= $ {TEMPDIR} /stderr.txt
記錄不少 stdout:$ {result.stdout} stderr:$ {result.stderr}    
$ {result} = 運行流程 程序 標準錯誤= STDOUT  
日誌 全部輸出:$ {result.stdout}      

請注意,測試運行後不會自動刪除建立的輸出文件。若是須要,用戶有責任將其刪除。

輸出編碼

默認狀況下,執行的命令但願使用系統控制檯使用的編碼將輸出寫入標準輸出和錯誤流。若是該命令使用某些其餘編碼,則可使用該output_encoding參數進行配置。這在Windows上使用與系統其餘部分不一樣的編碼時很是有用,而且許多命令使用通用系統編碼而不是控制檯編碼。

output_encoding參數一塊兒使用的值必須是有效編碼,而且必須與命令實際使用的編碼匹配。爲方便起見,可使用字符串CONSOLESYSTEM分別指定使用控制檯或系統編碼。若是生成的輸出使用隨後配置的不一樣編碼,則經過結果對象得到的值將無效。

例子:

開始流程 程序 output_encoding = UTF-8  
運行流程 程序 標準輸出= $ {路徑} output_encoding = SYSTEM

設置輸出編碼的支持是Robot Framework 3.0中的新增功能。

別號

選擇活動進程時可使用的自定義名稱。

例子:

開始流程 程序 別名=示例    
運行流程 蟒蛇 -C 打印'你好' 別名=你好

積極的過程

測試庫記錄哪些已啓動的進程當前處於活動狀態。默認狀況下,它是使用Start Process啓動的最新流程,但Switch Process可用於選擇其餘流程。使用「運行進程 」不會影響活動進程。

默認狀況下,對已啓動進程執行操做的關鍵字將使用活動進程,但可使用該handle參數顯式選擇其餘進程。該手柄能夠是該標識符由返回開始處理alias顯式地提供給啓動過程運行過程

結果對象

「運行進程」,「 等待進程」和「 終止進程」關鍵字返回一個結果對象,該對象包含有關進程執行的信息做爲其屬性。使用Get Process Result關鍵字也能夠得到相同的結果對象或其某些屬性。對象中可用的屬性記錄在下表中。

屬性 說明
RC 將進程的代碼做爲整數返回。
標準輸出 標準輸出流的內容。
標準錯誤 標準錯誤流的內容。
stdout_path stdout被重定向或未重定向的路徑None
stderr_path 重定向stderr或未重定向的路徑None

例:

$ {result} = 運行流程 程序
應該與整數相等 $ {} result.rc 0
應該匹配 $ {} result.stdout 一些t?xt *
應該是空的 $ {} result.stderr  
$ {stdout} = 獲取文件 $ {} result.stdout_path
應該是平等的 $ {}標準輸出 $ {} result.stdout
文件應該是空的 $ {} result.stderr_path  

布爾參數

某些關鍵字接受以布爾值true或false處理的參數。若是這樣的參數以字符串形式給出,則若是它是空字符串或不區分大小寫,則被視爲false falsenoneno。不管其值如何,其餘字符串都被視爲true,其餘參數類型使用與Python相同的規則進行測試。

真實的例子:

終止流程 殺=真 #字符串一般是正確的。
終止流程 殺= YES #與上述相同。
終止流程 殺= $ {TRUE} #Pcthon True是真的。
終止流程 殺= $ {42} #0之外的數字爲真。

錯誤的例子:

終止流程 殺=假 #String false爲false。
終止流程 殺=無 #字符串no也是false。
終止流程 殺= $ {EMPTY} #Empty字符串爲false。
終止流程 殺= $ {FALSE} #Python False是假的。

在Robot Framework 2.9以前,全部非空字符串(包括falseno)都被認爲是真的。none在Robot Framework 3.0.3中考慮false是新的。

***設置*** 庫進程 套件拆解 終止全部進程 kill = True ***測試用例*** 示例啓動進程 程序arg1 arg2 alias = First $ {handle} = 啓動進程 command.sh arg | command2.sh shell = True cwd = / path $ {result} = 運行流程 $ {CURDIR} /script.py 不該包含 $ {result.stdout} FAIL 終止流程 $ {handle} $ {result} = 等待流程 優先 應該與整數相等 $ {result.rc} 0 

快捷鍵

獲取進程ID · 獲取進程對象 · 獲取進程結果 · 進程正在運行 · 加入命令行 · 進程應該運行 · 進程應該中止 · 運行進程 · 發送信號進行處理 · 拆分命令行 · 啓動進程 · 切換進程 · 終止全部進程 · 終止進程 · 等待進程

關鍵詞

關鍵詞 參數 文檔
獲取進程ID 手柄=無

以整數形式返回進程的進程ID(pid)。

若是handle未給出,則使用當前活動進程

請注意,pid與此庫內部使用的Start Process返回的句柄不一樣。

獲取流程對象 手柄=無

返回底層subprocess.Popen對象。

若是handle未給出,則使用當前活動進程

獲取流程結果 handle = None, rc = False, stdout = False, stderr = False, stdout_path = False, stderr_path = False

返回指定的結果對象或其某些屬性。

給定handle指定應返回其結果的進程。若是不handle給出,則返回當前活動進程的結果。在任何一種狀況下,都必須在使用此關鍵字以前完成該過程。實際上,這意味着在使用此關鍵字以前,必須使用「 等待進程」或「 終止進程」完成以「 啓動進程」啓動的進程

若是沒有handle給出除可選以外的其餘參數,則返回整個結果對象。若是一個或多個其餘參數被賦予任何真值,則僅返回結果對象的指定屬性。始終以與關鍵字簽名中指定的參數相同的順序返回這些屬性。有關true和false值的更多詳細信息,請參閱布爾參數部分。

例子:

運行流程 蟒蛇 -C 打印'你好,世界!' 別名= myproc的  
#獲取結果對象          
$ {result} = 獲取流程結果 在myproc      
應該是平等的 $ {} result.rc $ {0}      
應該是平等的 $ {} result.stdout 你好,世界!      
應該是空的 $ {} result.stderr        
#獲取一個屬性          
$ {stdout} = 獲取流程結果 在myproc 標準輸出=真    
應該是平等的 $ {}標準輸出 你好,世界!      
#多個屬性          
$ {}標準輸出 $ {stderr} = 獲取流程結果 在myproc 標準輸出= YES 標準錯誤= YES
應該是平等的 $ {}標準輸出 你好,世界!      
應該是空的 $ {}標準錯誤        

雖然獲取先前執行的進程的結果一般很方便,但此關鍵字的主要用例是經過遠程庫接口返回結果。遠程接口不支持返回整個結果對象,但能夠毫無問題地返回各個屬性。

機器人框架2.8.2中的新功能。

流程正在運行 手柄=無

檢查是否正在運行。

若是handle未給出,則使用當前活動進程

True若是進程仍在運行,False則返回。

加入命令行 * ARGS

將參數鏈接到一個命令行字符串。

在結果命令行中,字符串參數用空格分隔,包含空格的參數用引號括起來,可能的引號用反斜槓轉義。

若是此關鍵字只給出一個參數,而且是一個像object這樣的列表,那麼將加入該列表的值。

例:

$ {cmd} = 加入命令行 - 選項 空格的價值
應該是平等的 $ {cmd},在 - 選擇「帶空格的價值」  

Robot Framework 2.9.2中的新功能。

流程應該運行 handle = None, error_message =進程未運行。

驗證進程是否正在運行。

若是handle未給出,則使用當前活動進程

若是進程已中止,則會失敗。

流程應該中止 handle = None, error_message =進程正在運行。

驗證進程未運行。

若是handle未給出,則使用當前活動進程

若是進程仍在運行,則會失敗。

運行流程 命令, *參數, **配置

運行一個進程並等待它完成。

command*arguments指定要執行的命令和傳遞給它的參數。有關更多詳細信息,請參閱指定命令和參數

**configuration包含與啓動進程和等待它們完成相關的其餘配置。有關與啓動進程相關的配置的詳細信息,請參閱進程配置。有關等待進程配置包括timeouton_timeout具備相同的語義與參數等待過程的關鍵字。默認狀況下沒有超時,若是定義了超時,則超時的默認操做爲terminate

返回包含有關執行信息的結果對象

請注意,*arguments必須使用反斜槓(例如name\=value)轉義可能的等號,以免它們被傳入**configuration

例子:

$ {result} = 運行流程 蟒蛇 -C 打印'你好,世界!'
應該是平等的 $ {} result.stdout 你好,世界!    
$ {result} = 運行流程 $ {}命令 標準錯誤= STDOUT 超時= 10S
$ {result} = 運行流程 $ {}命令 超時= 1分鐘 on_timeout =繼續
$ {result} = 運行流程 java -Dname \ = value示例 殼=真 CWD = $ {實施例}

此關鍵字不會更改活動進程

timeouton_timeout參數是Robot Framework 2.8.4中的新功能。

發送信號進行處理 signal, handle = None, group = False

將給定發送signal到指定的進程。

若是handle未給出,則使用當前活動進程

能夠將信號指定爲整數做爲信號名稱。在後一種狀況下,能夠給出帶或不帶SIG前綴的名稱,但名稱區分大小寫。例如,如下全部示例都發送信號INT (2)

發送信號進行處理 2   #發送到活動進程
發送信號進行處理 INT    
發送信號進行處理 SIGINT 在myproc #發送到命名進程

此關鍵字僅在類Unix機器上支持,而不在Windows上支持。支持哪些信號取決於系統。有關係統上現有信號的列表,請參閱與信號處理相關的Unix手冊頁(一般man signalman 7 signal)。

默認狀況下,僅將信號發送到父進程,而不是發送給它啓動的子進程。請注意,在shell中運行進程時,shell是父進程,它取決於系統shell是否將信號傳播到實際啓動的進程。

要將信號發送到整個進程組,group能夠將參數設置爲任何真值(請參閱布爾參數)。可是,Jython不支持此功能。

機器人框架2.8.2中的新功能。支持group參數是Robot Framework 2.8.5中的新功能。

拆分命令行 args, escaping = False

將命令行字符串拆分爲參數列表。

字符串從空格中拆分,但用引號括起來的參數可能包含空格。若是escaping給出一個真值,則反斜槓被視爲轉義字符。它能夠轉義不帶引號的空格,引號內的引號等,但在使用Windows路徑時也須要使用雙反斜槓。

例子:

@ {cmd} = 拆分命令行 - 選擇「帶空格的價值」
應該是真的 $ cmd == [' - 選項','帶空格的值']  

Robot Framework 2.9.2中的新功能。

開始流程 命令, *參數, **配置

在後臺啓動新流程。

有關參數的詳細信息,請參閱指定命令和參數以及進程配置 ;有關相關示例,請參閱Run Process關鍵字。

使啓動的進程成爲新的活動進程。返回一個標識符,該標識符可用做句柄以在須要時激活已啓動的進程。

從Robot Framework 2.8.5開始,啓動進程以便建立新的進程組。這容許向可能的子進程發送信號並終止它們。Jython通常也不支持這種方法,也不支持Windows上2.7以前的Python版本。

切換過程 處理

使指定的進程成爲當前活動進程

該手柄能夠是由返回的標識符開始處理alias明確地給它。

例:

開始流程 PROG1 別名=過程1
開始流程 PROG2 別名=過程2
#當前活動的進程是process2    
切換過程 過程1  
#now active進程是process1    
終止全部進程 殺=假

終止此庫啓動的全部仍在運行的進程。

此關鍵字能夠在套件拆解或其餘地方使用,以確保全部進程都已中止,

默認狀況下嘗試正常終止進程,但能夠配置爲當即強制終止它們。有關詳細信息,請參閱此關鍵字在內部使用的終止進程

終止流程 handle = None, kill = False

優雅地或強制地中止該過程。

若是handle未給出,則使用當前活動進程

默認狀況下,首先嚐試正常中止該過程。若是進程在30秒內沒有中止,或者kill參數被賦予真值,則(參見布爾參數)強制終止進程。中止最初啓動的進程的全部子進程。

在終止進程後等待進程中止。返回一個結果對象,其中包含與Wait For Process相似的執行信息。

在相似Unix的機器上,使用TERM (15)信號和kill進行優雅終止KILL (9)。若是您只想發送這些信號中的任何一個而不等待進程中止,請使用發送信號來處理

在Windows上CTRL_BREAK_EVENT,使用Win32 API函數使用事件和查殺完成正常終止TerminateProcess()

例子:

$ {result} = 終止流程    
應該與整數相等 $ {} result.rc -15 #在Unix上
終止流程 在myproc 殺=真  

限制:

  • Windows上不支持Jython的優雅終止,也不支持2.7以前的Python版本。進程被殺死了。
  • Jython根本不支持中止整個進程組,也不支持Windows上2.7以前的Python版本。
  • 在Windows上強制kill只會中止主進程,而不是子進程。

若是終止失敗以及返回結果對象,則自動終止進程是Robot Framework 2.8.2中的新功能。終止可能的子進程,包括CTRL_BREAK_EVENT在Windows上使用,是Robot Framework 2.8.5中的新功能。

等待過程 handle = None, timeout = None, on_timeout = continue

等待進程完成或達到給定的超時。

必須先使用「 啓動過程」啓動等待過程。若是handle未給出,則使用當前活動進程

timeout定義等待進程的最長時間。它能夠被賦予不一樣的時間格式由機器人框架的支持,例如4242 s1 minute 30 seconds

on_timeout定義超時發生時要執行的操做。可能的值和相應的操做在下表中說明。請注意,達到超時永遠不會失敗測試。

行動
繼續 該過程保持運行(默認)。
終止 該過程優雅地終止。
該過程被強制中止。

有關如何終止和終止進程的詳細信息,請參閱Terminate Process關鍵字。

若是進程在超時以前結束或者終止或終止,則此關鍵字返回包含有關執行信息的結果對象。若是進程保持運行,None則返回Python 。

例子:

#流程完全結束      
$ {result} = 等待過程  
流程應該中止    
應該與整數相等 $ {} result.rc 0  
#進程沒有結束      
$ {result} = 等待過程 超時= 42秒  
流程應該運行      
應該是平等的 $ {}結果 $ {無}  
#終止不結束的過程      
$ {result} = 等待過程 超時= 1分30秒 on_timeout =殺
流程應該中止      
應該與整數相等 $ {} result.rc -9  

timeout而且on_timeout是Robot Framework 2.8.2中的新功能。

共有15個關鍵字。 
Libdoc於2018-04-25 23:41:29 生成。

相關文章
相關標籤/搜索