RobotFramework之OperatingSystem

OperatingSystem

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

Introduction

A test library providing keywords for OS related tasks.html

OperatingSystem is Robot Framework's standard library that enables various operating system related tasks to be performed in the system where Robot Framework is running. It can, among other things, execute commands (e.g. Run), create and remove files and directories (e.g. Create FileRemove Directory), check whether files or directories exists or contain something (e.g. File Should ExistDirectory Should Be Empty) and manipulate environment variables (e.g. Set Environment Variable).python

Table of contents

Path separators

Because Robot Framework uses the backslash (\) as an escape character in the test data, using a literal backslash requires duplicating it like in c:\\path\\file.txt. That can be inconvenient especially with longer Windows paths, and thus all keywords expecting paths as arguments convert forward slashes to backslashes automatically on Windows. This also means that paths like ${CURDIR}/path/file.txt are operating system independent.app

Notice that the automatic path separator conversion does not work if the path is only a part of an argument like with Run and Start Process keywords. In these cases the built-in variable ${/} that contains \ or /, depending on the operating system, can be used instead.框架

Pattern matching

Some keywords allow their arguments to be specified as glob patterns where:less

* matches anything, even an empty string
? matches any single character
[chars] matches any character inside square brackets (e.g. [abc] matches either ab or c)
[!chars] matches any character not inside square brackets

Unless otherwise noted, matching is case-insensitive on case-insensitive operating systems such as Windows. Pattern matching is implemented using fnmatch module.ide

Starting from Robot Framework 2.9.1, globbing is not done if the given path matches an existing file even if it would contain a glob pattern.測試

Tilde expansion

Paths beginning with ~ or ~username are expanded to the current or specified user's home directory, respectively. The resulting path is operating system dependent, but typically e.g. ~/robot is expanded to C:\Users\<user>\robot on Windows and /home/<user>/robot on Unixes.ui

Tilde expansion is a new feature in Robot Framework 2.8. The ~username form does not work on Jythonthis

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:

Remove Directory ${path} recursive=True # Strings are generally true.
Remove Directory ${path} recursive=yes # Same as the above.
Remove Directory ${path} recursive=${TRUE} # Python True is true.
Remove Directory ${path} recursive=${42} # Numbers other than 0 are true.

False examples:

Remove Directory ${path} recursive=False # String false is false.
Remove Directory ${path} recursive=no # Also string no is false.
Remove Directory ${path} recursive=${EMPTY} # Empty string is false.
Remove Directory ${path} recursive=${FALSE} # Python False is false.

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

Example

Setting Value
Library OperatingSystem
Variable Value
${PATH} ${CURDIR}/example.txt
Test Case Action Argument Argument
Example Create File ${PATH} Some text
  File Should Exist ${PATH}  
  Copy File ${PATH} ~/file.txt
  ${output} = Run ${TEMPDIR}${/}script.py arg

Shortcuts

Append To Environment Variable · Append To File · Copy Directory · Copy File · Copy Files · Count Directories In Directory · Count Files In Directory · Count Items In Directory · Create Binary File · Create Directory · Create File ·Directory Should Be Empty · Directory Should Exist · Directory Should Not Be Empty · Directory Should Not Exist · Empty Directory · Environment Variable Should Be Set · Environment Variable Should Not Be Set · File Should Be Empty ·File Should Exist · File Should Not Be Empty · File Should Not Exist · Get Binary File · Get Environment Variable · Get Environment Variables · Get File · Get File Size · Get Modified Time · Grep File · Join Path · Join Paths ·List Directories In Directory · List Directory · List Files In Directory · Log Environment Variables · Log File · Move Directory · Move File · Move Files · Normalize Path · Remove Directory · Remove Environment Variable · Remove File ·Remove Files · Run · Run And Return Rc · Run And Return Rc And Output · Set Environment Variable · Set Modified Time · Should Exist · Should Not Exist · Split Extension · Split Path · Touch · Wait Until Created · Wait Until Removed

Keywords

Keyword Arguments Documentation
Append To Environment Variable name, *values, **config

Appends given values to environment variable name.

If the environment variable already exists, values are added after it, and otherwise a new environment variable is created.

Values are, by default, joined together using the operating system path separator (; on Windows, : elsewhere). This can be changed by giving a separator after the values like separator=value. No other configuration parameters are accepted.

Examples (assuming NAME and NAME2 do not exist initially):

Append To Environment Variable NAME first  
Should Be Equal %{NAME} first  
Append To Environment Variable NAME second third
Should Be Equal %{NAME} first${:}second${:}third  
Append To Environment Variable NAME2 first separator=-
Should Be Equal %{NAME2} first  
Append To Environment Variable NAME2 second separator=-
Should Be Equal %{NAME2} first-second  

New in Robot Framework 2.8.4.

Append To File path, content,encoding=UTF-8

Appends the given content to the specified file.

If the file does not exists, this keyword works exactly the same way as Create File.

Copy Directory source, destination

Copies the source directory into the destination.

If the destination exists, the source is copied under it. Otherwise the destination directory and the possible missing intermediate directories are created.

Copy File source, destination

Copies the source file into the destination.

Source must be an existing file. Starting from Robot Framework 2.8.4, it can be given as a glob pattern (see Pattern matching) that matches exactly one file. How the destination is interpreted is explained below.

1) If the destination is an existing file, the source file is copied over it.

2) If the destination is an existing directory, the source file is copied into it. A possible file with the same name as the source is overwritten.

3) If the destination does not exist and it ends with a path separator (/ or \), it is considered a directory. That directory is created and a source file copied into it. Possible missing intermediate directories are also created.

4) If the destination does not exist and it does not end with a path separator, it is considered a file. If the path to the file does not exist, it is created.

The resulting destination path is returned since Robot Framework 2.9.2.

See also Copy FilesMove File, and Move Files.

Copy Files *sources_and_destination

Copies specified files to the target directory.

Source files can be given as exact paths and as glob patterns (see Pattern matching). At least one source must be given, but it is not an error if it is a pattern that does not match anything.

Last argument must be the destination directory. If the destination does not exist, it will be created.

Examples:

Copy Files ${dir}/file1.txt ${dir}/file2.txt ${dir2}
Copy Files ${dir}/file-*.txt ${dir2}  

See also Copy FileMove File, and Move Files.

New in Robot Framework 2.8.4.

Count Directories In Directory path, pattern=None

Wrapper for Count Items In Directory returning only directory count.

Count Files In Directory path, pattern=None

Wrapper for Count Items In Directory returning only file count.

Count Items In Directory path, pattern=None

Returns and logs the number of all items in the given directory.

The argument pattern has the same semantics as with List Directory keyword. The count is returned as an integer, so it must be checked e.g. with the built-in keyword Should Be Equal As Integers.

Create Binary File path, content

Creates a binary file with the given content.

If content is given as a Unicode string, it is first converted to bytes character by character. All characters with ordinal below 256 can be used and are converted to bytes with same values. Using characters with higher ordinal is an error.

Byte strings, and possible other types, are written to the file as is.

If the directory for the file does not exist, it is created, along with missing intermediate directories.

Examples:

Create Binary File ${dir}/example.png ${image content}
Create Binary File ${path} \x01\x00\xe4\x00

Use Create File if you want to create a text file using a certain encoding. File Should Not Exist can be used to avoid overwriting existing files.

New in Robot Framework 2.8.5.

Create Directory path

Creates the specified directory.

Also possible intermediate directories are created. Passes if the directory already exists, but fails if the path exists and is not a directory.

Create File path, content=,encoding=UTF-8

Creates a file with the given content and encoding.

If the directory for the file does not exist, it is created, along with missing intermediate directories.

See Get File for more information about possible encoding values, including special values SYSTEM and CONSOLE.

Examples:

Create File ${dir}/example.txt Hello, world!  
Create File ${path} Hyv\xe4 esimerkki Latin-1
Create File /tmp/foo.txt ${content} SYSTEM

Use Append To File if you want to append to an existing file and Create Binary File if you need to write bytes without encoding. File Should Not Exist can be used to avoid overwriting existing files.

The support for SYSTEM and CONSOLE encodings is new in Robot Framework 3.0.

Directory Should Be Empty path, msg=None

Fails unless the specified directory is empty.

The default error message can be overridden with the msg argument.

Directory Should Exist path, msg=None

Fails unless the given path points to an existing directory.

The path can be given as an exact path or as a glob pattern. The pattern matching syntax is explained in introduction. The default error message can be overridden with the msg argument.

Directory Should Not Be Empty path, msg=None

Fails if the specified directory is empty.

The default error message can be overridden with the msg argument.

Directory Should Not Exist path, msg=None

Fails if the given path points to an existing file.

The path can be given as an exact path or as a glob pattern. The pattern matching syntax is explained in introduction. The default error message can be overridden with the msg argument.

Empty Directory path

Deletes all the content from the given directory.

Deletes both files and sub-directories, but the specified directory itself if not removed. Use Remove Directory if you want to remove the whole directory.

Environment Variable Should Be Set name, msg=None

Fails if the specified environment variable is not set.

The default error message can be overridden with the msg argument.

Environment Variable Should Not Be Set name, msg=None

Fails if the specified environment variable is set.

The default error message can be overridden with the msg argument.

File Should Be Empty path, msg=None

Fails unless the specified file is empty.

The default error message can be overridden with the msg argument.

File Should Exist path, msg=None

Fails unless the given path points to an existing file.

The path can be given as an exact path or as a glob pattern. The pattern matching syntax is explained in introduction. The default error message can be overridden with the msg argument.

File Should Not Be Empty path, msg=None

Fails if the specified directory is empty.

The default error message can be overridden with the msg argument.

File Should Not Exist path, msg=None

Fails if the given path points to an existing file.

The path can be given as an exact path or as a glob pattern. The pattern matching syntax is explained in introduction. The default error message can be overridden with the msg argument.

Get Binary File path

Returns the contents of a specified file.

This keyword reads the specified file and returns the contents as is. See also Get File.

Get Environment Variable name, default=None

Returns the value of an environment variable with the given name.

If no such environment variable is set, returns the default value, if given. Otherwise fails the test case.

Starting from Robot Framework 2.7, returned variables are automatically decoded to Unicode using the system encoding.

Note that you can also access environment variables directly using the variable syntax %{ENV_VAR_NAME}.

Get Environment Variables  

Returns currently available environment variables as a dictionary.

Both keys and values are decoded to Unicode using the system encoding. Altering the returned dictionary has no effect on the actual environment variables.

New in Robot Framework 2.7.

Get File path, encoding=UTF-8,encoding_errors=strict

Returns the contents of a specified file.

This keyword reads the specified file and returns the contents. Line breaks in content are converted to platform independent form. See also Get Binary File.

encoding defines the encoding of the file. The default value is UTF-8, which means that UTF-8 and ASCII encoded files are read correctly. In addition to the encodings supported by the underlying Python implementation, the following special encoding values can be used:

  • SYSTEM: Use the default system encoding.
  • CONSOLE: Use the console encoding. Outside Windows this is same as the system encoding.

encoding_errors argument controls what to do if decoding some bytes fails. All values accepted by decode method in Python are valid, but in practice the following values are most useful:

  • strict: Fail if characters cannot be decoded (default).
  • ignore: Ignore characters that cannot be decoded.
  • replace: Replace characters that cannot be decoded with a replacement character.

encoding_errors argument was added in Robot Framework 2.8.5 and the support for SYSTEM and CONSOLE encodings in Robot Framework 3.0.

Get File Size path

Returns and logs file size as an integer in bytes.

Get Modified Time path, format=timestamp

Returns the last modification time of a file or directory.

How time is returned is determined based on the given format string as follows. Note that all checks are case-insensitive. Returned time is also automatically logged.

1) If format contains the word epoch, the time is returned in seconds after the UNIX epoch. The return value is always an integer.

2) If format contains any of the words yearmonthdayhourmin or sec, only the selected parts are returned. The order of the returned parts is always the one in the previous sentence and the order of the words in format is not significant. The parts are returned as zero-padded strings (e.g. May -> 05).

3) Otherwise, and by default, the time is returned as a timestamp string in the format 2006-02-24 15:08:31.

Examples (when the modified time of ${CURDIR} is 2006-03-29 15:06:21):

${time} = Get Modified Time ${CURDIR}    
${secs} = Get Modified Time ${CURDIR} epoch  
${year} = Get Modified Time ${CURDIR} return year  
${y} ${d} = Get Modified Time ${CURDIR} year,day
@{time} = Get Modified Time ${CURDIR} year,month,day,hour,min,sec  

=>

  • ${time} = '2006-03-29 15:06:21'
  • ${secs} = 1143637581
  • ${year} = '2006'
  • ${y} = '2006' & ${d} = '29'
  • @{time} = ['2006', '03', '29', '15', '06', '21']
Grep File path, pattern,encoding=UTF-8,encoding_errors=strict

Returns the lines of the specified file that match the pattern.

This keyword reads a file from the file system using the defined pathencoding and encoding_errors similarly as Get File. A difference is that only the lines that match the given pattern are returned. Lines are returned as a single string catenated back together with newlines and the number of matched lines is automatically logged. Possible trailing newline is never returned.

A line matches if it contains the pattern anywhere in it and it does not need to match the pattern fully. The pattern matching syntax is explained in introduction, and in this case matching is case-sensitive.

Examples:

${errors} = Grep File /var/log/myapp.log ERROR
${ret} = Grep File ${CURDIR}/file.txt [Ww]ildc??d ex*ple

If more complex pattern matching is needed, it is possible to use Get File in combination with String library keywords like Get Lines Matching Regexp.

encoding_errors argument is new in Robot Framework 2.8.5.

Join Path base, *parts

Joins the given path part(s) to the given base path.

The path separator (/ or \) is inserted when needed and the possible absolute paths handled as expected. The resulted path is also normalized.

Examples:

${path} = Join Path my path    
${p2} = Join Path my/ path/    
${p3} = Join Path my path my file.txt
${p4} = Join Path my /path    
${p5} = Join Path /my/path/ .. path2  

=>

  • ${path} = 'my/path'
  • ${p2} = 'my/path'
  • ${p3} = 'my/path/my/file.txt'
  • ${p4} = '/path'
  • ${p5} = '/my/path2'
Join Paths base, *paths

Joins given paths with base and returns resulted paths.

See Join Path for more information.

Examples:

@{p1} = Join Paths base example other  
@{p2} = Join Paths /my/base /example other  
@{p3} = Join Paths my/base example/path/ other one/more

=>

  • @{p1} = ['base/example', 'base/other']
  • @{p2} = ['/example', '/my/base/other']
  • @{p3} = ['my/base/example/path', 'my/base/other', 'my/base/one/more']
List Directories In Directory path, pattern=None,absolute=False

Wrapper for List Directory that returns only directories.

List Directory path, pattern=None,absolute=False

Returns and logs items in a directory, optionally filtered with pattern.

File and directory names are returned in case-sensitive alphabetical order, e.g. ['A Name', 'Second', 'a lower case name', 'one more']. Implicit directories . and .. are not returned. The returned items are automatically logged.

File and directory names are returned relative to the given path (e.g. 'file.txt') by default. If you want them be returned in absolute format (e.g. '/home/robot/file.txt'), give the absolute argument a true value (see Boolean arguments).

If pattern is given, only items matching it are returned. The pattern matching syntax is explained in introduction, and in this case matching is case-sensitive.

Examples (using also other List Directory variants):

@{items} = List Directory ${TEMPDIR}    
@{files} = List Files In Directory /tmp *.txt absolute
${count} = Count Files In Directory ${CURDIR} ???  
List Files In Directory path, pattern=None,absolute=False

Wrapper for List Directory that returns only files.

Log Environment Variables level=INFO

Logs all environment variables using the given log level.

Environment variables are also returned the same way as with Get Environment Variables keyword.

New in Robot Framework 2.7.

Log File path, encoding=UTF-8,encoding_errors=strict

Wrapper for Get File that also logs the returned file.

The file is logged with the INFO level. If you want something else, just use Get File and the built-in keyword Log with the desired level.

See Get File for more information about encoding and encoding_errors arguments.

encoding_errors argument is new in Robot Framework 2.8.5.

Move Directory source, destination

Moves the source directory into a destination.

Uses Copy Directory keyword internally, and source and destination arguments have exactly same semantics as with that keyword.

Move File source, destination

Moves the source file into the destination.

Arguments have exactly same semantics as with Copy File keyword. Destination file path is returned since Robot Framework 2.9.2.

If the source and destination are on the same filesystem, rename operation is used. Otherwise file is copied to the destination filesystem and then removed from the original filesystem.

See also Move FilesCopy File, and Copy Files.

Move Files *sources_and_destination

Moves specified files to the target directory.

Arguments have exactly same semantics as with Copy Files keyword.

See also Move FileCopy File, and Copy Files.

New in Robot Framework 2.8.4.

Normalize Path path

Normalizes the given path.

Examples:

${path} = Normalize Path abc
${p2} = Normalize Path abc/
${p3} = Normalize Path abc/../def
${p4} = Normalize Path abc/./def
${p5} = Normalize Path abc//def

=>

  • ${path} = 'abc'
  • ${p2} = 'abc'
  • ${p3} = 'def'
  • ${p4} = 'abc/def'
  • ${p5} = 'abc/def'
Remove Directory path, recursive=False

Removes the directory pointed to by the given path.

If the second argument recursive is given a true value (see Boolean arguments), the directory is removed recursively. Otherwise removing fails if the directory is not empty.

If the directory pointed to by the path does not exist, the keyword passes, but it fails, if the path points to a file.

Remove Environment Variable *names

Deletes the specified environment variable.

Does nothing if the environment variable is not set.

Starting from Robot Framework 2.7, it is possible to remove multiple variables by passing them to this keyword as separate arguments.

Remove File path

Removes a file with the given path.

Passes if the file does not exist, but fails if the path does not point to a regular file (e.g. it points to a directory).

The path can be given as an exact path or as a glob pattern. The pattern matching syntax is explained in introduction. If the path is a pattern, all files matching it are removed.

Remove Files *paths

Uses Remove File to remove multiple files one-by-one.

Example:

Remove Files ${TEMPDIR}${/}foo.txt ${TEMPDIR}${/}bar.txt ${TEMPDIR}${/}zap.txt
Run command

Runs the given command in the system and returns the output.

The execution status of the command is not checked by this keyword, and it must be done separately based on the returned output. If the execution return code is needed, either Run And Return RC or Run And Return RC And Output can be used.

The standard error stream is automatically redirected to the standard output stream by adding 2>&1 after the executed command. This automatic redirection is done only when the executed command does not contain additional output redirections. You can thus freely forward the standard error somewhere else, for example, like my_command 2>stderr.txt.

The returned output contains everything written into the standard output or error streams by the command (unless either of them is redirected explicitly). Many commands add an extra newline (\n) after the output to make it easier to read in the console. To ease processing the returned output, this possible trailing newline is stripped by this keyword.

Examples:

${output} = Run ls -lhF /tmp
Log ${output}  
${result} = Run ${CURDIR}${/}tester.py arg1 arg2
Should Not Contain ${result} FAIL
${stdout} = Run /opt/script.sh 2>/tmp/stderr.txt
Should Be Equal ${stdout} TEST PASSED
File Should Be Empty /tmp/stderr.txt  

TIP: Run Process keyword provided by the Process library supports better process configuration and is generally recommended as a replacement for this keyword.

Run And Return Rc command

Runs the given command in the system and returns the return code.

The return code (RC) is returned as a positive integer in range from 0 to 255 as returned by the executed command. On some operating systems (notable Windows) original return codes can be something else, but this keyword always maps them to the 0-255 range. Since the RC is an integer, it must be checked e.g. with the keyword Should Be Equal As Integers instead of Should Be Equal (both are built-in keywords).

Examples:

${rc} = Run and Return RC ${CURDIR}${/}script.py arg
Should Be Equal As Integers ${rc} 0
${rc} = Run and Return RC /path/to/example.rb arg1 arg2
Should Be True 0 < ${rc} < 42  

See Run and Run And Return RC And Output if you need to get the output of the executed command.

TIP: Run Process keyword provided by the Process library supports better process configuration and is generally recommended as a replacement for this keyword.

Run And Return Rc And Output command

Runs the given command in the system and returns the RC and output.

The return code (RC) is returned similarly as with Run And Return RC and the output similarly as with Run.

Examples:

${rc} ${output} = Run and Return RC and Output ${CURDIR}${/}mytool
Should Be Equal As Integers ${rc} 0  
Should Not Contain ${output} FAIL  
${rc} ${stdout} = Run and Return RC and Output /opt/script.sh 2>/tmp/stderr.txt
Should Be True ${rc} > 42    
Should Be Equal ${stdout} TEST PASSED  
File Should Be Empty /tmp/stderr.txt    

TIP: Run Process keyword provided by the Process library supports better process configuration and is generally recommended as a replacement for this keyword.

Set Environment Variable name, value

Sets an environment variable to a specified value.

Values are converted to strings automatically. Starting from Robot Framework 2.7, set variables are automatically encoded using the system encoding.

Set Modified Time path, mtime

Sets the file modification and access times.

Changes the modification and access times of the given file to the value determined by mtime. The time can be given in different formats described below. Note that all checks involving strings are case-insensitive. Modified time can only be set to regular files.

1) If mtime is a number, or a string that can be converted to a number, it is interpreted as seconds since the UNIX epoch (1970-01-01 00:00:00 UTC). This documentation was originally written about 1177654467 seconds after the epoch.

2) If mtime is a timestamp, that time will be used. Valid timestamp formats are YYYY-MM-DD hh:mm:ss and YYYYMMDD hhmmss.

3) If mtime is equal to NOW, the current local time is used. This time is got using Python's time.time() function.

4) If mtime is equal to UTC, the current time in UTC is used. This time is got using time.time() + time.altzone in Python.

5) If mtime is in the format like NOW - 1 day or UTC + 1 hour 30 min, the current local/UTC time plus/minus the time specified with the time string is used. The time string format is described in an appendix of Robot Framework User Guide.

Examples:

Set Modified Time /path/file 1177654467 # Time given as epoch seconds
Set Modified Time /path/file 2007-04-27 9:14:27 # Time given as a timestamp
Set Modified Time /path/file NOW # The local time of execution
Set Modified Time /path/file NOW - 1 day # 1 day subtracted from the local time
Set Modified Time /path/file UTC + 1h 2min 3s # 1h 2min 3s added to the UTC time

Support for UTC time is a new feature in Robot Framework 2.7.5.

Should Exist path, msg=None

Fails unless the given path (file or directory) exists.

The path can be given as an exact path or as a glob pattern. The pattern matching syntax is explained in introduction. The default error message can be overridden with the msg argument.

Should Not Exist path, msg=None

Fails if the given path (file or directory) exists.

The path can be given as an exact path or as a glob pattern. The pattern matching syntax is explained in introduction. The default error message can be overridden with the msg argument.

Split Extension path

Splits the extension from the given path.

The given path is first normalized (e.g. possible trailing path separators removed, special directories .. and . removed). The base path and extension are returned as separate components so that the dot used as an extension separator is removed. If the path contains no extension, an empty string is returned for it. Possible leading and trailing dots in the file name are never considered to be extension separators.

Examples:

${path} ${ext} = Split Extension file.extension
${p2} ${e2} = Split Extension path/file.ext
${p3} ${e3} = Split Extension path/file
${p4} ${e4} = Split Extension p1/../p2/file.ext
${p5} ${e5} = Split Extension path/.file.ext
${p6} ${e6} = Split Extension path/.file

=>

  • ${path} = 'file' & ${ext} = 'extension'
  • ${p2} = 'path/file' & ${e2} = 'ext'
  • ${p3} = 'path/file' & ${e3} = ''
  • ${p4} = 'p2/file' & ${e4} = 'ext'
  • ${p5} = 'path/.file' & ${e5} = 'ext'
  • ${p6} = 'path/.file' & ${e6} = ''
Split Path path

Splits the given path from the last path separator (/ or \).

The given path is first normalized (e.g. a possible trailing path separator is removed, special directories .. and . removed). The parts that are split are returned as separate components.

Examples:

${path1} ${dir} = Split Path abc/def
${path2} ${file} = Split Path abc/def/ghi.txt
${path3} ${d2} = Split Path abc/../def/ghi/

=>

  • ${path1} = 'abc' & ${dir} = 'def'
  • ${path2} = 'abc/def' & ${file} = 'ghi.txt'
  • ${path3} = 'def' & ${d2} = 'ghi'
Touch path

Emulates the UNIX touch command.

Creates a file, if it does not exist. Otherwise changes its access and modification times to the current time.

Fails if used with the directories or the parent directory of the given file does not exist.

Wait Until Created path, timeout=1 minute

Waits until the given file or directory is created.

The path can be given as an exact path or as a glob pattern. The pattern matching syntax is explained in introduction. If the path is a pattern, the keyword returns when an item matching it is created.

The optional timeout can be used to control the maximum time of waiting. The timeout is given as a timeout string, e.g. in a format 15 seconds1min 10s or just 10. The time string format is described in an appendix of Robot Framework User Guide.

If the timeout is negative, the keyword is never timed-out. The keyword returns immediately, if the path already exists.

Wait Until Removed path, timeout=1 minute

Waits until the given file or directory is removed.

The path can be given as an exact path or as a glob pattern. The pattern matching syntax is explained in introduction. If the path is a pattern, the keyword waits until all matching items are removed.

The optional timeout can be used to control the maximum time of waiting. The timeout is given as a timeout string, e.g. in a format 15 seconds1min 10s or just 10. The time string format is described in an appendix of Robot Framework User Guide.

If the timeout is negative, the keyword is never timed-out. The keyword returns immediately, if the path does not exist in the first place.

Altogether 56 keywords. 
Generated by Libdoc on 2018-04-25 23:41:28.

操做系統

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

介紹

爲OS相關任務提供關鍵字的測試庫。

OperatingSystem是Robot Framework的標準庫,能夠在運行Robot Framework的系統中執行各類與操做系統相關的任務。除其餘外,它能夠執行命令(例如運行),建立和刪除文件和目錄(例如建立文件刪除目錄),檢查文件或目錄是否存在或包含某些內容(例如文件應該存在目錄應該爲空)和操縱環境變量(例如設置環境變量)。

目錄

路徑分隔符

由於Robot Framework \在測試數據中使用反斜槓()做爲轉義字符,因此使用文字反斜槓須要像在中同樣複製它c:\\path\\file.txt。這可能不方便,特別是對於較長的Windows路徑,所以全部但願路徑做爲參數的關鍵字會在Windows上自動將正斜槓轉換爲反斜槓。這也意味着路徑就像${CURDIR}/path/file.txt操做系統無關。

請注意,若是路徑只是參數的一部分(如RunStart Process關鍵字),則自動路徑分隔符轉換不起做用。在這些狀況下,可使用${/}包含\/取決於操做系統的內置變量。

模式匹配

有些關鍵字容許將其參數指定爲glob模式,其中:

* 匹配任何東西,甚至是空字符串
? 匹配任何單個字符
[chars] 匹配方括號內的任何字符(例如[abc]匹配abc
[!chars] 匹配不在方括號內的任何字符

除非另有說明,不然匹配在不區分大小寫的操做系統(如Windows)上不區分大小寫。使用fnmatch模塊實現模式匹配。

從Robot Framework 2.9.1開始,若是給定路徑與現有文件匹配,即便它包含glob模式,也不會進行globbing。

Tilde擴張

分別以當前或指定用戶的主目錄開頭~~username擴展的路徑。生成的路徑取決於操做系統,但一般例如在Windows和Unix 上~/robot擴展。C:\Users\<user>\robot/home/<user>/robot

Tilde擴展是Robot Framework 2.8中的一項新功能。該~username表單不適用於Jython

布爾參數

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

真實的例子:

刪除目錄 $ {PATH} 遞歸=真 #字符串一般是正確的。
刪除目錄 $ {PATH} 遞歸= YES #與上述相同。
刪除目錄 $ {PATH} 遞歸= $ {TRUE} #Pcthon True是真的。
刪除目錄 $ {PATH} 遞歸= $ {42} #0之外的數字爲真。

錯誤的例子:

刪除目錄 $ {PATH} 遞歸=假 #String false爲false。
刪除目錄 $ {PATH} 遞歸=無 #字符串no也是false。
刪除目錄 $ {PATH} 遞歸= $ {EMPTY} #Empty字符串爲false。
刪除目錄 $ {PATH} 遞歸= $ {FALSE} #Python False是假的。

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

設置
圖書館 操做系統
變量
$ {PATH} $ {} CURDIR /example.txt
測試用例 行動 論據 論據
建立文件 $ {PATH} 一些文字
  文件應該存在 $ {PATH}  
  複製文件 $ {PATH} 〜/ file.txt的
  $ {output} = $ {TEMPDIR} $ {/} script.py arg

快捷鍵

追加到環境變量 · 附加到文件 · 複製目錄 · 複製文件 · 複製文件 · 伯爵目錄,在目錄 · 計數目錄中的文件 · 清點貨物目錄 · 建立二進制文件 · 建立目錄 · 建立文件 · 目錄應該是空的 · 目錄應該存在 · 目錄不該該是空的 · 目錄不該該存在 · 空目錄 · 應該設置環境變量 · 應該設置環境變量 · 文件應該是空的 · 文件應該存在 · 文件不該該是空的 · 文件不該該存在 · 獲取二進制文件 · 獲取環境變量 · 獲取環境變量 · 獲取文件 · 獲取文件大小 · 獲取修改時間 · Grep文件 · 加入路徑 · 加入路徑 · 列出目錄中的目錄 ·列表目錄 · 列出目錄中的文件 · 日誌環境變量 · 日誌文件 · 移動目錄 · 移動文件 · 移動文件 · 規範化路徑 · 刪除目錄 · 刪除環境變量 · 刪除文件 · 刪除文件 · 運行 · 運行並返回Rc · 運行並返回Rc和輸出 · 設置環境變量 · 設置修改時間 · 應該存在 · 不該存在 ·拆分擴展 · 拆分路徑 · 觸摸 · 等待直到建立 · 等待直到刪除

關鍵詞

關鍵詞 參數 文檔
附加到環境變量 name, * values, ** config

附加values到環境變量name

若是環境變量已存在,則在其後添加值,不然將建立新的環境變量。

默認狀況下,值使用操做系統路徑分隔符(;在Windows上,:其餘位置)鏈接在一塊兒。這能夠經過在值以後給出一個分隔符來改變separator=value。不接受其餘配置參數。

示例(假設NAME而且NAME2最初不存在):

附加到環境變量 名稱 第一  
應該是平等的 %{名稱} 第一  
附加到環境變量 名稱 第二 第三
應該是平等的 %{名稱} 第一$ {:}第二$ {:}第三  
附加到環境變量 NAME2 第一 隔板= -
應該是平等的 %{} NAME2 第一  
附加到環境變量 NAME2 第二 隔板= -
應該是平等的 %{} NAME2 第一秒  

Robot Framework 2.8.4中的新功能。

附加到文件 路徑, 內容, 編碼= UTF-8

將給定內容附加到指定文件。

若是該文件不存在,則此關鍵字的工做方式與「 建立文件」徹底相同。

複製目錄 來源, 目的地

將源目錄複製到目標。

若是目標存在,則在其下複製源。不然,將建立目標目錄和可能缺乏的中間目錄。

複製文件 來源, 目的地

將源文件複製到目標。

源必須是現有文件。從Robot Framework 2.8.4開始,它能夠做爲一個徹底匹配一個文件的glob模式(參見模式匹配)給出。下面解釋如何解釋目的地。

1)若是目標是現有文件,則將源文件複製到其上。

2)若是目標是現有目錄,則將源文件複製到其中。將覆蓋與源名稱相同的可能文件。

3)若是目標不存在而且以路徑分隔符(/\)結束,則將其視爲目錄。建立該目錄並將源文件複製到其中。還會建立可能缺乏的中間目錄。

4)若是目標不存在且它不以路徑分隔符結束,則將其視爲文件。若是文件的路徑不存在,則建立該路徑。

從Robot Framework 2.9.2開始返回結果目標路徑。

另請參閱複製文件移動文件移動文件

複製文件 * sources_and_destination

將指定的文件複製到目標目錄。

源文件能夠做爲精確路徑和glob模式給出(請參閱模式匹配)。必須至少給出一個源,但若是它是一個與任何東西都不匹配的模式,則不是錯誤。

最後一個參數必須是目標目錄。若是目標不存在,則將建立該目標。

例子:

複製文件 $ {DIR} /file1.txt $ {DIR} /file2.txt $ {} DIR2
複製文件 $ {DIR} /文件 - * TXT $ {} DIR2  

另請參閱複製文件移動文件移動文件

Robot Framework 2.8.4中的新功能。

計算目錄中的目錄 path, pattern = None

計數項目的包裝器在目錄中僅返回目錄計數。

計算目錄中的文件 path, pattern = None

計數項目的包裝在目錄中僅返回文件計數。

計算目錄中的項目 path, pattern = None

返回並記錄給定目錄中全部項的數量。

該參數patternList Directory關鍵字具備相同的語義。計數以整數形式返回,所以必須使用內置關鍵字「 Be Be Equal As Integers」進行檢查

建立二進制文件 路徑, 內容

建立具備給定內容的二進制文件。

若是內容以Unicode字符串形式給出,則首先將其逐個字符轉換爲字節。可使用序號低於256的全部字符,並將其轉換爲具備相同值的字節。使用序號較高的字符是錯誤的。

字節字符串和可能的其餘類型將按原樣寫入文件。

若是該文件的目錄不存在,則會建立該目錄以及缺乏的中間目錄。

例子:

建立二進制文件 $ {DIR} /example.png $ {image content}
建立二進制文件 $ {PATH} \ X01 \ X00 \ XE4 \ X00

若是要使用特定編碼建立文本文件,請使用「 建立文件」File Not Not Exist可用於避免覆蓋現有文件。

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

建立目錄 路徑

建立指定的目錄。

還能夠建立可能的中間目錄。若是目錄已存在則經過,但若是路徑存在且不是目錄則失敗。

建立文件 path, content =, encoding = UTF-8

建立具備給定內容和編碼的文件。

若是該文件的目錄不存在,則會建立該目錄以及缺乏的中間目錄。

有關可能的值的更多信息,請參閱獲取文件encoding,包括特殊值SYSTEMCONSOLE

例子:

建立文件 $ {DIR} /example.txt 你好,世界!  
建立文件 $ {PATH} Hyv \ xe4 esimerkki 拉丁-1
建立文件 /tmp/foo.txt $ {}內容 系統

若是要在不編碼的狀況下編寫字節,則若是要附加到現有文件並建立二進制文件,請使用「 附加到文件」File Not Not Exist可用於避免覆蓋現有文件。

對Robot Framework 3.0 的支持SYSTEMCONSOLE編碼是新的。

目錄應該是空的 path, msg =無

除非指定的目錄爲空,不然失敗。

可使用msg參數覆蓋默認錯誤消息。

目錄應該存在 path, msg =無

除非給定路徑指向現有目錄,不然失敗。

路徑能夠做爲精確路徑或全局模式給出。模式匹配語法在介紹中進行了解釋。可使用msg參數覆蓋默認錯誤消息。

目錄不該該是空的 path, msg =無

若是指定的目錄爲空,則失敗。

可使用msg參數覆蓋默認錯誤消息。

目錄不該該存在 path, msg =無

若是給定路徑指向現有文件,則會失敗。

路徑能夠做爲精確路徑或全局模式給出。模式匹配語法在介紹中進行了解釋。可使用msg參數覆蓋默認錯誤消息。

空目錄 路徑

刪除給定目錄中的全部內容。

刪除文件和子目錄,但若是未刪除則刪除指定的目錄。若是要刪除整個目錄,請使用「 刪除目錄」。

應該設置環境變量 name, msg =無

若是未設置指定的環境變量,則會失敗。

可使用msg參數覆蓋默認錯誤消息。

不該設置環境變量 name, msg =無

若是設置了指定的環境變量,則會失敗。

可使用msg參數覆蓋默認錯誤消息。

文件應該是空的 path, msg =無

除非指定的文件爲空,不然失敗。

可使用msg參數覆蓋默認錯誤消息。

文件應該存在 path, msg =無

除非給定path指向現有文件,不然失敗。

路徑能夠做爲精確路徑或全局模式給出。模式匹配語法在介紹中進行了解釋。可使用msg參數覆蓋默認錯誤消息。

文件不該該是空的 path, msg =無

若是指定的目錄爲空,則失敗。

可使用msg參數覆蓋默認錯誤消息。

文件不該該存在 path, msg =無

若是給定路徑指向現有文件,則會失敗。

路徑能夠做爲精確路徑或全局模式給出。模式匹配語法在介紹中進行了解釋。可使用msg參數覆蓋默認錯誤消息。

獲取二進制文件 路徑

返回指定文件的內容。

此關鍵字讀取指定的文件並按原樣返回內容。另請參閱獲取文件

獲取環境變量 name, default = None

返回具備給定名稱的環境變量的值。

若是未設置此類環境變量,則返回默認值(若是給定)。不然測試用例失敗。

從Robot Framework 2.7開始,返回的變量使用系統編碼自動解碼爲Unicode。

請注意,您還可使用變量語法直接訪問環境變量%{ENV_VAR_NAME}

獲取環境變量  

將當前可用的環境變量做爲字典返回。

使用系統編碼將鍵和值都解碼爲Unicode。更改返回的字典對實際環境變量沒有影響。

Robot Framework 2.7中的新功能。

獲取文件 path, encoding = UTF-8encoding_errors = strict

返回指定文件的內容。

此關鍵字讀取指定的文件並返回內容。內容中的換行符將轉換爲與平臺無關的表單。另請參閱獲取二進制文件

encoding定義文件的編碼。默認值爲UTF-8,表示正確讀取UTF-8和ASCII編碼文件。除了底層Python實現支持的編碼以外,還可使用如下特殊編碼值:

  • SYSTEM:使用默認系統編碼。
  • CONSOLE:使用控制檯編碼。在Windows以外,這與系統編碼相同。

encoding_errors若是解碼某些字節失敗,參數控制該怎麼作。decodePython中方法接受的全部值都是有效的,但實際上如下值最有用:

  • strict:若是沒法解碼字符,則失敗(默認)。
  • ignore:忽略沒法解碼的字符。
  • replace:替換沒法使用替換字符解碼的字符。

encoding_errors在Robot Framework 2.8.5中添加了參數,SYSTEMCONSOLE在Robot Framework 3.0中支持和編碼。

獲取文件大小 路徑

返回並將文件大小記錄爲整數(以字節爲單位)。

得到修改時間 path, format = timestamp

返回文件或目錄的最後修改時間。

如何返回時間是根據給定的format字符串肯定的,以下所示。請注意,全部檢查都不區分大小寫。返回時間也會自動記錄。

1)若是format包含單詞epoch,則在UNIX紀元後以秒爲單位返回時間。返回值始終爲整數。

2)若是format包含任何的話yearmonthdayhourminsec,僅選定部分被返回。返回部分的順序始終是前一句中的順序,單詞的順序format並不重要。這些部分做爲零填充字符串返回(例如May - > 05)。

3)不然,默認狀況下,時間將做爲格式的時間戳字符串返回2006-02-24 15:08:31

例子(當修改時間${CURDIR}是2006-03-29 15:06:21):

$ {time} = 得到修改時間 $ {} CURDIR    
$ {secs} = 得到修改時間 $ {} CURDIR 時代  
$ {year} = 得到修改時間 $ {} CURDIR 迴歸年  
$ {Y} $ {d} = 得到修改時間 $ {} CURDIR 年,日
@ {time} = 得到修改時間 $ {} CURDIR 年,月,日,時,分,秒  

=>

  • $ {time} ='2006-03-29 15:06:21'
  • $ {secs} = 1143637581
  • $ {year} ='2006'
  • $ {y} ='2006'&$ {d} = '29'
  • @ {time} = ['2006','03','29','15','06','21']
Grep文件 path, pattern, encoding = UTF-8, encoding_errors = strict

返回與之匹配的指定文件的行pattern

此關鍵字讀取使用定義從文件系統中的文件pathencoding而且encoding_errors一樣爲獲取文件。不一樣之處在於只pattern返回與給定匹配的行。行返回爲與換行符一塊兒鏈接的單個字符串,並自動記錄匹配行的數量。永遠不會返回可能的尾隨換行符。

若是一行包含其中的pattern任何位置而且不須要徹底匹配該模式,則該行匹配。模式匹配語法在介紹中進行了解釋,在這種狀況下,匹配區分大小寫。

例子:

$ {errors} = Grep文件 /var/log/myapp.log 錯誤
$ {ret} = Grep文件 $ {} CURDIR /file.txt [Ww] ildc ?? d ex * ple

若是須要更復雜的模式匹配,則能夠將Get File與字符串庫關鍵字(如Get Lines Matching Regexp)結合使用。

encoding_errors 參數是Robot Framework 2.8.5中的新功能。

加入路徑 基地, *部分

將給定的路徑部分鏈接到給定的基本路徑。

在須要時插入路徑分隔符(/\),並按預期處理可能的絕對路徑。結果路徑也被標準化。

例子:

$ {path} = 加入路徑 個人 路徑    
$ {p2} = 加入路徑 個人/ 路徑/    
$ {p3} = 加入路徑 個人 路徑 個人 file.txt的
$ {p4} = 加入路徑 個人 /路徑    
$ {p5} = 加入路徑 /我本身的路/ .. 路徑2  

=>

  • $ {path} ='my / path'
  • $ {p2} ='我/路徑'
  • $ {p3} ='my / path / my / file.txt'
  • $ {p4} ='/ path'
  • $ {p5} ='/ my / path2'
加入路徑 基地, *路徑

使用base鏈接給定路徑並返回結果路徑。

有關更多信息,請參閱加入路徑

例子:

@ {p1} = 加入路徑 基礎 其餘  
@ {p2} = 加入路徑 /個人/基 /例 其餘  
@ {p3} = 加入路徑 我/基 例如/路徑/ 其餘 多一個

=>

  • @ {p1} = ['base / example','base / other']
  • @ {p2} = ['/ example','/ my / base / other']
  • @ {p3} = ['my / base / example / path','my / base / other','my / base / one / more']
列出目錄中的目錄 path, pattern = None, absolute = False

列表目錄的包裝器,僅返回目錄。

列表目錄 path, pattern = None, absolute = False

返回並記錄目錄中的項目,可選擇使用pattern

文件和目錄名稱以區分大小寫的字母順序返回,例如['A Name', 'Second', 'a lower case name', 'one more']。隱式目錄.而且..不會返回。返回的項目將自動記錄。

'file.txt'默認狀況下,相對於給定路徑(例如)返回文件和目錄名稱。若是您但願以絕對格式返回它們(例如'/home/robot/file.txt'),請爲absolute參數賦予一個真值(請參閱布爾參數)。

若是pattern給出,則僅返回與其匹配的項目。模式匹配語法在介紹中進行了解釋,在這種狀況下,匹配區分大小寫。

示例(使用其餘列表目錄變體):

@ {items} = 列表目錄 $ {TEMPDIR}    
@ {files} = 列出目錄中的文件 / tmp目錄 *。文本 絕對
$ {count} = 計算目錄中的文件 $ {} CURDIR ???  
列出目錄中的文件 path, pattern = None, absolute = False

列表目錄的包裝器,僅返回文件。

記錄環境變量 級= INFO

使用給定的日誌級別記錄全部環境變量。

環境變量的返回方式與Get Environment Variables關鍵字的返回方式相同。

Robot Framework 2.7中的新功能。

日誌文件 path, encoding = UTF-8encoding_errors = strict

Get File的包裝器也記錄返回的文件。

使用INFO級別記錄該文件。若是您須要其餘內容,只需使用Get File和內置關鍵字Log以及所需級別。

有關和參數的更多信息,請參閱獲取文件encodingencoding_errors

encoding_errors 參數是Robot Framework 2.8.5中的新功能。

移動目錄 來源, 目的地

將源目錄移動到目標。

使用複製目錄關鍵字內部,而且sourcedestination參數都徹底同樣的語義與該關鍵字。

移動文件 來源, 目的地

將源文件移動到目標。

參數與「 複製文件」關鍵字具備徹底相同的語義。從Robot Framework 2.9.2開始返回目標文件路徑。

若是源和目標位於同一文件系統上,則使用重命名操做。不然,文件將複製到目標文件系統,而後從原始文件系統中刪除。

另請參閱移動文件複製文件複製文件

移動文件 * sources_and_destination

將指定的文件移動到目標目錄。

參數與Copy Files關鍵字具備徹底相同的語義。

另請參閱移動文件複製文件複製文件

Robot Framework 2.8.4中的新功能。

規範化路徑 路徑

規範化給定路徑。

例子:

$ {path} = 規範化路徑 ABC
$ {p2} = 規範化路徑 ABC /
$ {p3} = 規範化路徑 ABC /../高清
$ {p4} = 規範化路徑 ABC /./高清
$ {p5} = 規範化路徑 ABC //高清

=>

  • $ {path} ='abc'
  • $ {p2} ='abc'
  • $ {p3} ='def'
  • $ {p4} ='abc / def'
  • $ {p5} ='abc / def'
刪除目錄 path, recursive = False

刪除給定指向的目錄path

若是給第二個參數recursive一個真值(參見布爾參數),則遞歸刪除該目錄。不然,若是目錄不爲空,則刪除失敗。

若是指向的目錄path不存在,則關鍵字傳遞,但若是path指向文件則失敗。

刪除環境變量 *名

刪除指定的環境變量。

若是未設置環境變量,則不執行任何操做。

從Robot Framework 2.7開始,能夠經過將多個變量做爲單獨的參數傳遞給此關鍵字來刪除它們。

刪除文件 路徑

刪除具備給定路徑的文件。

若是文件不存在則經過,但若是路徑未指向常規文件(例如,它指向目錄)則失敗。

路徑能夠做爲精確路徑或全局模式給出。模式匹配語法在介紹中進行了解釋。若是路徑是模式,則刪除與其匹配的全部文件。

刪除文件 *路徑

使用「 刪除文件」逐個刪除多個文件。

例:

刪除文件 $ {TEMPDIR} $ {/} foo.txt的 $ {TEMPDIR} $ {/}跳回到bar.txt $ {TEMPDIR} $ {/} zap.txt
命令

在系統中運行給定命令並返回輸出。

此關鍵字不檢查命令的執行狀態,必須根據返回的輸出單獨完成。若是須要執行返回代碼,可使用Run And Return RCRun And Return RC和Output

經過2>&1在執行的命令以後添加,標準錯誤流自動重定向到標準輸出流。僅當執行的命令不包含其餘輸出重定向時,纔會執行此自動重定向。所以,您能夠在其餘地方自由轉發標準錯誤,例如my_command 2>stderr.txt

返回的輸出包含命令寫入標準輸出或錯誤流的全部內容(除非其中任何一個顯式重定向)。許多命令\n在輸出後添加一個額外的換行符(),以便在控制檯中更容易閱讀。爲了便於處理返回的輸出,此關鍵字將刪除此可能的尾隨換行符。

例子:

$ {output} = ls -lhF / tmp
日誌 $ {}輸出  
$ {result} = $ {CURDIR} $ {/} tester.py arg1 arg2
不該該包含 $ {}結果 失敗
$ {stdout} = /opt/script.sh 2> /tmp/stderr.txt
應該是平等的 $ {}標準輸出 考試經過了
文件應該是空的 /tmp/stderr.txt  

提示: Process庫提供的Run Process關鍵字支持更好的流程配置,一般建議將其替換爲此關鍵字。

運行並返回Rc 命令

在系統中運行給定命令並返回返回碼。

返回代碼(RC)做爲執行命令返回的0到255範圍內的正整數返回。在某些操做系統(着名的Windows)上,原始返回代碼多是其餘內容,但此關鍵字始終將它們映射到0-255範圍。因爲RC是一個整數,所以必須使用關鍵字Do Be Equal As Integers而不是Should Be Equal(二者都是內置關鍵字)進行檢查。

例子:

$ {rc} = 運行並返回RC $ {CURDIR} $ {/} script.py arg
應該與整數相等 $ {} RC 0
$ {rc} = 運行並返回RC /path/to/example.rb arg1 arg2
應該是真的 0 <$ {rc} <42  

若是須要獲取已執行命令的輸出,請參閱運行運行並返回RC和輸出

提示: Process庫提供的Run Process關鍵字支持更好的流程配置,一般建議將其替換爲此關鍵字。

運行並返回Rc和輸出 命令

在系統中運行給定命令並返回RC和輸出。

Run和Return RC相似地返回返回代碼(RC),輸出與Run相似。

例子:

$ {} RC $ {output} = 運行並返回RC和輸出 $ {CURDIR} $ {/} mytool
應該與整數相等 $ {} RC 0  
不該該包含 $ {}輸出 失敗  
$ {} RC $ {stdout} = 運行並返回RC和輸出 /opt/script.sh 2> /tmp/stderr.txt
應該是真的 $ {rc}> 42    
應該是平等的 $ {}標準輸出 考試經過了  
文件應該是空的 /tmp/stderr.txt    

提示: Process庫提供的Run Process關鍵字支持更好的流程配置,一般建議將其替換爲此關鍵字。

設置環境變量 名稱, 價值

將環境變量設置爲指定值。

值將自動轉換爲字符串。從Robot Framework 2.7開始,使用系統編碼自動編碼設置變量。

設置修改時間 路徑, mtime

設置文件修改和訪問時間。

將給定文件的修改和訪問時間更改成由肯定的值mtime。時間能夠如下面描述的不一樣格式給出。請注意,涉及字符串的全部檢查都不區分大小寫。修改時間只能設置爲常規文件。

1)若是mtime是數字或能夠轉換爲數字的字符串,則將其解釋爲自UNIX紀元(1970-01-01 00:00:00 UTC)以來的秒數。該文檔最初是在紀元後的1177654467秒寫的。

2)若是mtime是時間戳,將使用該時間。有效的時間戳格式爲YYYY-MM-DD hh:mm:ssYYYYMMDD hhmmss

3)若是mtime等於NOW,則使用當前本地時間。此次是使用Python的time.time()功能。

4)若是mtime等於UTC,則使用UTC中的當前時間。此次是time.time() + time.altzone在Python中使用的。

5)若是mtime格式爲NOW - 1 dayUTC + 1 hour 30 min,則使用當前本地/ UTC時間加/減時間字符串指定的時間。時間字符串格式在Robot Framework用戶指南的附錄中描述。

例子:

設置修改時間 /路徑/文件 1177654467 #給出時間秒的時間
設置修改時間 /路徑/文件 2007-04-27 9:14:27 #做爲時間戳給出的時間
設置修改時間 /路徑/文件 如今 #本地執行時間
設置修改時間 /路徑/文件 如今 - 1天 從當地時間減去#1天
設置修改時間 /路徑/文件 UTC + 1h 2min 3s #1h 2min 3s添加到UTC時間

支持UTC時間是Robot Framework 2.7.5中的一項新功能。

應該存在 path, msg =無

除非給定的路徑(文件或目錄)存在,不然失敗。

路徑能夠做爲精確路徑或全局模式給出。模式匹配語法在介紹中進行了解釋。可使用msg參數覆蓋默認錯誤消息。

不該該存在 path, msg =無

若是給定的路徑(文件或目錄)存在,則失敗。

路徑能夠做爲精確路徑或全局模式給出。模式匹配語法在介紹中進行了解釋。可使用msg參數覆蓋默認錯誤消息。

拆分擴展 路徑

從給定路徑拆分擴展。

給定的路徑首先被歸一化(例如,可能的拖尾路徑分隔除去,特殊目錄...移除)。基本路徑和擴展名做爲單獨的組件返回,以便刪除用做擴展分隔符的點。若是路徑不包含擴展名,則爲其返回空字符串。文件名中可能的前導和尾隨點永遠不會被視爲擴展分隔符。

例子:

$ {PATH} $ {ext} = 拆分擴展 文件擴展名
$ {} P2 $ {e2} = 拆分擴展 路徑/ file.ext
$ {} P3 $ {e3} = 拆分擴展 路徑/文件
$ {} P4 $ {e4} = 拆分擴展 P1 /../ P2 / file.ext
$ {} P5 $ {e5} = 拆分擴展 路徑/ .file.ext
$ {} P6 $ {e6} = 拆分擴展 路徑/ .file

=>

  • $ {path} ='file'&$ {ext} ='extension'
  • $ {p2} ='路徑/文件'&$ {e2} ='ext'
  • $ {p3} ='路徑/文件'和$ {e3} =''
  • $ {p4} ='p2 / file'&$ {e4} ='ext'
  • $ {p5} ='path / .file'和$ {e5} ='ext'
  • $ {p6} ='path / .file'和$ {e6} =''
拆分路徑 路徑

從最後一個路徑分隔符(/\)拆分給定路徑。

給定的路徑首先被歸一化(例如,可能的拖尾路徑分隔被去除,特殊目錄...移除)。拆分的部件做爲單獨的組件返回。

例子:

$ {PATH1} $ {dir} = 拆分路徑 ABC / DEF
$ {} PATH2 $ {file} = 拆分路徑 ABC / DEF / ghi.txt
$ {} path3時, $ {d2} = 拆分路徑 ABC DEF /../ / GHI /

=>

  • $ {path1} ='abc'和$ {dir} ='def'
  • $ {path2} ='abc / def'和$ {file} ='ghi.txt'
  • $ {path3} ='def'&$ {d2} ='ghi'
觸摸 路徑

模擬UNIX touch命令。

若是文件不存在,則建立該文件。不然將其訪問和修改時間更改成當前時間。

若是與目錄一塊兒使用或給定文件的父目錄不存在,則會失敗。

等到建立 路徑, 超時= 1分鐘

等待直到建立給定文件或目錄。

路徑能夠做爲精確路徑或全局模式給出。模式匹配語法在介紹中進行了解釋。若是路徑是模式,則關鍵字在建立與其匹配的項目時返回。

可選項timeout可用於控制最長等待時間。超時給出超時字符串,例如在一個格式15 seconds1min 10s或者只10。時間字符串格式在Robot Framework用戶指南的附錄中描述。

若是超時爲負,則關鍵字永遠不會超時。若是路徑已存在,則關鍵字當即返回。

等到刪除 路徑, 超時= 1分鐘

等待直到刪除給定的文件或目錄。

路徑能夠做爲精確路徑或全局模式給出。模式匹配語法在介紹中進行了解釋。若是路徑是模式,則關鍵字將等待,直到刪除全部匹配項。

可選項timeout可用於控制最長等待時間。超時給出超時字符串,例如在一個格式15 seconds1min 10s或者只10。時間字符串格式在Robot Framework用戶指南的附錄中描述。

若是超時爲負,則關鍵字永遠不會超時。若是路徑首先不存在,則關鍵字當即返回。

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

相關文章
相關標籤/搜索