RobotFramework之Telnet

Telnet

Library version: 3.0.4
Library scope: test suite
Named arguments: supported

Introduction

A test library providing communication over Telnet connections.html

Telnet is Robot Framework's standard library that makes it possible to connect to Telnet servers and execute commands on the opened connections.python

Table of contents

Connections

The first step of using Telnet is opening a connection with Open Connection keyword. Typically the next step is logging in with Login keyword, and in the end the opened connection can be closed with Close Connection.正則表達式

It is possible to open multiple connections and switch the active one using Switch ConnectionClose All Connections can be used to close all the connections, which is especially useful in suite teardowns to guarantee that all connections are always closed.express

Writing and reading

After opening a connection and possibly logging in, commands can be executed or text written to the connection for other reasons using Write and Write Bare keywords. The main difference between these two is that the former adds a configurable newline after the text automatically.緩存

After writing something to the connection, the resulting output can be read using ReadRead UntilRead Until Regexp, and Read Until Prompt keywords. Which one to use depends on the context, but the latest one is often the most convenient.服務器

As a convenience when running a command, it is possible to use Execute Command that simply uses Write and Read Until Prompt internally. Write Until Expected Output is useful if you need to wait until writing something produces a desired output.app

Written and read text is automatically encoded/decoded using a configured encoding.框架

The ANSI escape codes, like cursor movement and color codes, are normally returned as part of the read operation. If an escape code occurs in middle of a search pattern it may also prevent finding the searched string. Terminal emulation can be used to process these escape codes as they would be if a real terminal would be in use.less

Configuration

Many aspects related the connections can be easily configured either globally or per connection basis. Global configuration is done when library is imported, and these values can be overridden per connection by Open Connection or with setting specific keywords Set TimeoutSet NewlineSet PromptSet EncodingSet Default Log Level and Set Telnetlib Log Level.ide

Values of environ_userwindow_sizeterminal_emulation, and terminal_type can not be changed after opening the connection.

Timeout

Timeout defines how long is the maximum time to wait when reading output. It is used internally by Read UntilRead Until RegexpRead Until Prompt, and Login keywords. The default value is 3 seconds.

Connection Timeout

Connection Timeout defines how long is the maximum time to wait when opening the telnet connection. It is used internally by Open Connection. The default value is the system global default timeout.

New in Robot Framework 2.9.2.

Newline

Newline defines which line separator Write keyword should use. The default value is CRLF that is typically used by Telnet connections.

Newline can be given either in escaped format using \n and \r or with special LF and CR syntax.

Examples:

Set Newline \n
Set Newline CRLF

Prompt

Often the easiest way to read the output of a command is reading all the output until the next prompt with Read Until Prompt. It also makes it easier, and faster, to verify did Login succeed.

Prompt can be specified either as a normal string or a regular expression. The latter is especially useful if the prompt changes as a result of the executed commands. Prompt can be set to be a regular expression by giving prompt_is_regexp argument a true value (see Boolean arguments).

Examples:

Open Connection lolcathost prompt=$
Set Prompt (> |# ) prompt_is_regexp=true

Encoding

To ease handling text containing non-ASCII characters, all written text is encoded and read text decoded by default. The default encoding is UTF-8 that works also with ASCII. Encoding can be disabled by using a special encoding value NONE. This is mainly useful if you need to get the bytes received from the connection as-is.

Notice that when writing to the connection, only Unicode strings are encoded using the defined encoding. Byte strings are expected to be already encoded correctly. Notice also that normal text in test data is passed to the library as Unicode and you need to use variables to use bytes.

It is also possible to configure the error handler to use if encoding or decoding characters fails. Accepted values are the same that encode/decode functions in Python strings accept. In practice the following values are the most useful:

  • ignore: ignore characters that cannot be encoded (default)
  • strict: fail if characters cannot be encoded
  • replace: replace characters that cannot be encoded with a replacement character

Examples:

Open Connection lolcathost encoding=Latin1 encoding_errors=strict
Set Encoding ISO-8859-15    
Set Encoding errors=ignore    

Using UTF-8 encoding by default and being able to configure the encoding are new features in Robot Framework 2.7.6. In earlier versions only ASCII was supported and encoding errors were silently ignored. Robot Framework 2.7.7 added a possibility to specify the error handler, changed the default behavior back to ignoring encoding errors, and added the possibility to disable encoding.

Default log level

Default log level specifies the log level keywords use for logging unless they are given an explicit log level. The default value is INFO, and changing it, for example, to DEBUG can be a good idea if there is lot of unnecessary output that makes log files big.

Configuring default log level in importing and with Open Connection are new features in Robot Framework 2.7.6. In earlier versions only Set Default Log Level could be used.

Terminal type

By default the Telnet library does not negotiate any specific terminal type with the server. If a specific terminal type, for example vt100, is desired, the terminal type can be configured in importing and with Open Connection.

New in Robot Framework 2.8.2.

Window size

Window size for negotiation with the server can be configured when importing the library and with Open Connection.

New in Robot Framework 2.8.2.

USER environment variable

Telnet protocol allows the USER environment variable to be sent when connecting to the server. On some servers it may happen that there is no login prompt, and on those cases this configuration option will allow still to define the desired username. The option environ_user can be used in importing and with Open Connection.

New in Robot Framework 2.8.2.

Terminal emulation

Starting from Robot Framework 2.8.2, Telnet library supports terminal emulation with Pyte. Terminal emulation will process the output in a virtual screen. This means that ANSI escape codes, like cursor movements, and also control characters, like carriage returns and backspaces, have the same effect on the result as they would have on a normal terminal screen. For example the sequence acdc\x1b[3Dbba will result in output abba.

Terminal emulation is taken into use by giving terminal_emulation argument a true value (see Boolean arguments) either in the library initialization or with Open Connection.

As Pyte approximates vt-style terminal, you may also want to set the terminal type as vt100. We also recommend that you increase the window size, as the terminal emulation will break all lines that are longer than the window row length.

When terminal emulation is used, the newline and encoding can not be changed anymore after opening the connection.

Examples:

Open Connection lolcathost terminal_emulation=True terminal_type=vt100 window_size=400x100

As a prerequisite for using terminal emulation, you need to have Pyte installed. Due to backwards incompatible changes in Pyte, different Robot Framework versions support different Pyte versions:

  • Pyte 0.6 and newer are supported by Robot Framework 3.0.3. Latest Pyte version can be installed (or upgraded) with pip install --upgrade pyte.
  • Pyte 0.5.2 and older are supported by Robot Framework 3.0.2 and earlier. Pyte 0.5.2 can be installed with pip install pyte==0.5.2.

Logging

All keywords that read something log the output. These keywords take the log level to use as an optional argument, and if no log level is specified they use the configured default value.

The valid log levels to use are TRACEDEBUGINFO (default), and WARN. Levels below INFO are not shown in log files by default whereas warnings are shown more prominently.

The telnetlib module used by this library has a custom logging system for logging content it sends and receives. By default these messages are written using TRACE level. Starting with Robot Framework 2.8.7 the level is configurable with the telnetlib_log_level option either in the library initialization, to the Open Connection or by using the Set Telnetlib Log Level keyword to the active connection. Special level NONE con be used to disable the logging altogether.

Time string format

Timeouts and other times used must be given as a time string using format like 15 seconds or 1min 10s. If the timeout is given as just a number, for example, 10 or 1.5, it is considered to be seconds. The time string format is described in more detail in an appendix of Robot Framework User Guide.

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:

Open Connection lolcathost terminal_emulation=True # Strings are generally true.
Open Connection lolcathost terminal_emulation=yes # Same as the above.
Open Connection lolcathost terminal_emulation=${TRUE} # Python True is true.
Open Connection lolcathost terminal_emulation=${42} # Numbers other than 0 are true.

False examples:

Open Connection lolcathost terminal_emulation=False # String false is false.
Open Connection lolcathost terminal_emulation=no # Also string no is false.
Open Connection lolcathost terminal_emulation=${EMPTY} # Empty string is false.
Open Connection lolcathost terminal_emulation=${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.

Importing

Arguments Documentation
timeout=3 seconds, newline=CRLF, prompt=None, prompt_is_regexp=False, encoding=UTF-8, encoding_errors=ignore, default_log_level=INFO, window_size=None, environ_user=None,terminal_emulation=False, terminal_type=None, telnetlib_log_level=TRACE,connection_timeout=None

Telnet library can be imported with optional configuration parameters.

Configuration parameters are used as default values when new connections are opened with Open Connection keyword. They can also be overridden after opening the connection using the Set ... keywords. See these keywords as well as ConfigurationTerminal emulation and Logging sections above for more information about these parameters and their possible values.

See Time string format and Boolean arguments sections for information about using arguments accepting times and Boolean values, respectively.

Examples (use only one of these):

Setting Value Value Value Value Comment
Library Telnet       # default values
Library Telnet 5 seconds     # set only timeout
Library Telnet newline=LF encoding=ISO-8859-1   # set newline and encoding using named arguments
Library Telnet prompt=$     # set prompt
Library Telnet prompt=(> |# ) prompt_is_regexp=yes   # set prompt as a regular expression
Library Telnet terminal_emulation=True terminal_type=vt100 window_size=400x100 # use terminal emulation with defined window size and terminal type
Library Telnet telnetlib_log_level=NONE     # disable logging messages from the underlying telnetlib

Shortcuts

Close All Connections · Close Connection · Execute Command · Login · Open Connection · Read · Read Until · Read Until Prompt · Read Until Regexp · Set Default Log Level · Set Encoding · Set Newline · Set Prompt ·Set Telnetlib Log Level · Set Timeout · Switch Connection · Write · Write Bare · Write Control Character · Write Until Expected Output

Keywords

Keyword Arguments Documentation
Close All Connections  

Closes all open connections and empties the connection cache.

If multiple connections are opened, this keyword should be used in a test or suite teardown to make sure that all connections are closed. It is not an error is some of the connections have already been closed by Close Connection.

After this keyword, new indexes returned by Open Connection keyword are reset to 1.

Close Connection loglevel=None

Closes the current Telnet connection.

Remaining output in the connection is read, logged, and returned. It is not an error to close an already closed connection.

Use Close All Connections if you want to make sure all opened connections are closed.

See Logging section for more information about log levels.

Execute Command command, loglevel=None, strip_prompt=False

Executes the given command and reads, logs, and returns everything until the prompt.

This keyword requires the prompt to be configured either in importing or with Open Connection or Set Prompt keyword.

This is a convenience keyword that uses Write and Read Until Prompt internally. Following two examples are thus functionally identical:

${out} = Execute Command pwd
Write pwd
${out} = Read Until Prompt

See Logging section for more information about log levels and Read Until Prompt for more information about the strip_prompt parameter.

Login username, password, login_prompt=login: , password_prompt=Password: ,login_timeout=1 second, login_incorrect=Login incorrect

Logs in to the Telnet server with the given user information.

This keyword reads from the connection until the login_prompt is encountered and then types the given username. Then it reads until the password_prompt and types the given password. In both cases a newline is appended automatically and the connection specific timeout used when waiting for outputs.

How logging status is verified depends on whether a prompt is set for this connection or not:

1) If the prompt is set, this keyword reads the output until the prompt is found using the normal timeout. If no prompt is found, login is considered failed and also this keyword fails. Note that in this case both login_timeoutand login_incorrect arguments are ignored.

2) If the prompt is not set, this keywords sleeps until login_timeout and then reads all the output available on the connection. If the output contains login_incorrect text, login is considered failed and also this keyword fails. Both of these configuration parameters were added in Robot Framework 2.7.6. In earlier versions they were hard coded.

See Configuration section for more information about setting newline, timeout, and prompt.

Open Connection host, alias=None, port=23, timeout=None, newline=None, prompt=None,prompt_is_regexp=False, encoding=None, encoding_errors=None,default_log_level=None, window_size=None, environ_user=None,terminal_emulation=None, terminal_type=None, telnetlib_log_level=None,connection_timeout=None

Opens a new Telnet connection to the given host and port.

The timeoutnewlinepromptprompt_is_regexpencodingdefault_log_levelwindow_sizeenviron_userterminal_emulationterminal_type and telnetlib_log_level arguments get default values when the library is imported. Setting them here overrides those values for the opened connection. See ConfigurationTerminal emulation and Logging sections for more information about these parameters and their possible values.

Possible already opened connections are cached and it is possible to switch back to them using Switch Connection keyword. It is possible to switch either using explicitly given alias or using index returned by this keyword. Indexing starts from 1 and is reset back to it by Close All Connections keyword.

Read loglevel=None

Reads everything that is currently available in the output.

Read output is both returned and logged. See Logging section for more information about log levels.

Read Until expected, loglevel=None

Reads output until expected text is encountered.

Text up to and including the match is returned and logged. If no match is found, this keyword fails. How much to wait for the output depends on the configured timeout.

See Logging section for more information about log levels. Use Read Until Regexp if more complex matching is needed.

Read Until Prompt loglevel=None, strip_prompt=False

Reads output until the prompt is encountered.

This keyword requires the prompt to be configured either in importing or with Open Connection or Set Prompt keyword.

By default, text up to and including the prompt is returned and logged. If no prompt is found, this keyword fails. How much to wait for the output depends on the configured timeout.

If you want to exclude the prompt from the returned output, set strip_prompt to a true value (see Boolean arguments). If your prompt is a regular expression, make sure that the expression spans the whole prompt, because only the part of the output that matches the regular expression is stripped away.

See Logging section for more information about log levels.

Optionally stripping prompt is a new feature in Robot Framework 2.8.7.

Read Until Regexp *expected

Reads output until any of the expected regular expressions match.

This keyword accepts any number of regular expressions patterns or compiled Python regular expression objects as arguments. Text up to and including the first match to any of the regular expressions is returned and logged. If no match is found, this keyword fails. How much to wait for the output depends on the configured timeout.

If the last given argument is a valid log level, it is used as loglevel similarly as with Read Until keyword.

See the documentation of Python re module for more information about the supported regular expression syntax. Notice that possible backslashes need to be escaped in Robot Framework test data.

Examples:

Read Until Regexp (#|$)  
Read Until Regexp first_regexp second_regexp
Read Until Regexp \\d{4}-\\d{2}-\\d{2} DEBUG
Set Default Log Level level

Sets the default log level used for logging in the current connection.

The old default log level is returned and can be used to restore the log level later.

See Configuration section for more information about global and connection specific configuration.

Set Encoding encoding=None, errors=None

Sets the encoding to use for writing and reading in the current connection.

The given encoding specifies the encoding to use when written/read text is encoded/decoded, and errorsspecifies the error handler to use if encoding/decoding fails. Either of these can be omitted and in that case the old value is not affected. Use string NONE to disable encoding altogether.

See Configuration section for more information about encoding and error handlers, as well as global and connection specific configuration in general.

The old values are returned and can be used to restore the encoding and the error handler later. See Set Prompt for a similar example.

If terminal emulation is used, the encoding can not be changed on an open connection.

Setting encoding in general is a new feature in Robot Framework 2.7.6. Specifying the error handler and disabling encoding were added in 2.7.7.

Set Newline newline

Sets the newline used by Write keyword in the current connection.

The old newline is returned and can be used to restore the newline later. See Set Timeout for a similar example.

If terminal emulation is used, the newline can not be changed on an open connection.

See Configuration section for more information about global and connection specific configuration.

Set Prompt prompt, prompt_is_regexp=False

Sets the prompt used by Read Until Prompt and Login in the current connection.

If prompt_is_regexp is given a true value (see Boolean arguments), the given prompt is considered to be a regular expression.

The old prompt is returned and can be used to restore the prompt later.

Example:

${prompt} ${regexp} = Set Prompt $
Do Something      
Set Prompt ${prompt} ${regexp}  

See the documentation of Python re module for more information about the supported regular expression syntax. Notice that possible backslashes need to be escaped in Robot Framework test data.

See Configuration section for more information about global and connection specific configuration.

Set Telnetlib Log Level level

Sets the log level used for logging in the underlying telnetlib.

Note that telnetlib can be very noisy thus using the level NONE can shutdown the messages generated by this library.

New in Robot Framework 2.8.7.

Set Timeout timeout

Sets the timeout used for waiting output in the current connection.

Read operations that expect some output to appear (Read UntilRead Until RegexpRead Until PromptLogin) use this timeout and fail if the expected output does not appear before this timeout expires.

The timeout must be given in time string format. The old timeout is returned and can be used to restore the timeout later.

Example:

${old} = Set Timeout 2 minute 30 seconds
Do Something    
Set Timeout ${old}  

See Configuration section for more information about global and connection specific configuration.

Switch Connection index_or_alias

Switches between active connections using an index or an alias.

Aliases can be given to Open Connection keyword which also always returns the connection index.

This keyword returns the index of previous active connection.

Example:

Open Connection myhost.net    
Login john secret  
Write some command    
Open Connection yourhost.com 2nd conn  
Login root password  
Write another cmd    
${old index}= Switch Connection 1 # index
Write something    
Switch Connection 2nd conn   # alias
Write whatever    
Switch Connection ${old index}   # back to original
[Teardown] Close All Connections    

The example above expects that there were no other open connections when opening the first one, because it used index 1 when switching to the connection later. If you are not sure about that, you can store the index into a variable as shown below.

${index} = Open Connection myhost.net
Do Something    
Switch Connection ${index}  
Write text, loglevel=None

Writes the given text plus a newline into the connection.

The newline character sequence to use can be configured both globally and per connection basis. The default value is CRLF.

This keyword consumes the written text, until the added newline, from the output and logs and returns it. The given text itself must not contain newlines. Use Write Bare instead if either of these features causes a problem.

Note: This keyword does not return the possible output of the executed command. To get the output, one of the Read ... keywords must be used. See Writing and reading section for more details.

See Logging section for more information about log levels.

Write Bare text

Writes the given text, and nothing else, into the connection.

This keyword does not append a newline nor consume the written text. Use Write if these features are needed.

Write Control Character character

Writes the given control character into the connection.

The control character is prepended with an IAC (interpret as command) character.

The following control character names are supported: BRK, IP, AO, AYT, EC, EL, NOP. Additionally, you can use arbitrary numbers to send any control character.

Example:

Write Control Character BRK # Send Break command
Write Control Character 241 # Send No operation command
Write Until Expected Output text, expected, timeout, retry_interval, loglevel=None

Writes the given text repeatedly, until expected appears in the output.

text is written without appending a newline and it is consumed from the output before trying to find expected. If expected does not appear in the output within timeout, this keyword fails.

retry_interval defines the time to wait expected to appear before writing the text again. Consuming the written text is subject to the normal configured timeout.

Both timeout and retry_interval must be given in time string format. See Logging section for more information about log levels.

Example:

Write Until Expected Output ps -ef| grep myprocess\r\n myprocess
... 5 s 0.5 s

The above example writes command ps -ef | grep myprocess\r\n until myprocess appears in the output. The command is written every 0.5 seconds and the keyword fails if myprocess does not appear in the output in 5 seconds.

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

遠程登陸

圖書館版本: 3.0.4
圖書館範圍: 測試套件
命名參數: 支持的

介紹

經過Telnet鏈接提供通訊的測試庫。

Telnet 是Robot Framework的標準庫,能夠鏈接到Telnet服務器並在打開的鏈接上執行命令。

目錄

鏈接

使用的第一步驟Telnet是打開與鏈接打開鏈接關鍵字。一般,下一步是使用Login關鍵字登陸,最後可使用Close Connection關閉打開的鏈接。

可使用Switch Connection打開多個鏈接並切換活動鏈接關閉全部鏈接可用於關閉全部鏈接,這在套件拆卸中尤爲有用,可確保始終關閉全部鏈接。

寫做和閱讀

打開鏈接並可能登陸後,可使用WriteWrite Bare關鍵字執行命令或使用WriteWrite Bare關鍵字將文本寫入鏈接。這二者之間的主要區別在於前者在文本後自動添加可配置的換行符

在向鏈接寫入內容後,可使用ReadRead UntilRead Until RegexpRead Until Prompt關鍵字讀取結果輸出。使用哪個取決於上下文,但最新的一個一般是最方便的。

爲了方便運行命令,可使用只內部使用WriteRead Until Prompt的Execute Command。若是您須要等到寫入內容產生所需的輸出,則寫入直到預期輸出很是有用。

使用配置的編碼自動編碼/解碼寫入和讀取的文本。

ANSI轉義碼(如光標移動和顏色代碼)一般做爲讀取操做的一部分返回。若是轉義代碼出如今搜索模式的中間,它也可能阻止查找搜索到的字符串。終端仿真可用於處理這些轉義代碼,若是真正的終端將被使用的話。

組態

能夠在全局或每一個鏈接的基礎上輕鬆配置與鏈接相關的許多方面。導入庫時完成全局配置,而且能夠經過Open Connection或設置特定關鍵字Set TimeoutSet NewlineSet PromptSet EncodingSet Default Log LevelSet Telnetlib Log Level來覆蓋每一個鏈接的這些值。

的價值觀environ_userwindow_sizeterminal_emulation,和terminal_type沒法打開鏈接後更改。

時間到

超時定義讀取輸出時等待的最長時間。它由Read UntilRead Until RegexpRead Until PromptLogin關鍵字在內部使用。默認值爲3秒。

鏈接超時

鏈接超時定義打開telnet鏈接時等待的最長時間。它由Open Connection在內部使用。默認值是系統全局默認超時。

Robot Framework 2.9.2中的新功能。

新隊

換行符定義Write關鍵字應使用的行分隔符。默認值是CRLFTelnet鏈接一般使用的值。

新行可使用給定不管是在轉義格式\n\r或有特殊LFCR語法。

例子:

設置換行符 \ n
設置換行符 CRLF

提示

一般,讀取命令輸出的最簡單方法是讀取全部輸出,直到具備「 讀取直到提示」的下一個提示。它還能夠更輕鬆,更快速地驗證登陸是否成功。

提示能夠指定爲普通字符串或正則表達式。若是提示因爲執行的命令而改變,則後者特別有用。經過賦予prompt_is_regexp參數一個真值,能夠將提示設置爲正則表達式(請參閱布爾參數)。

例子:

打開鏈接 lolcathost 提示= $
設置提示 (> |#) prompt_is_regexp =真

編碼

爲了便於處理包含非ASCII字符的文本,默認狀況下對全部書寫文本進行編碼和讀取文本解碼。默認編碼是UTF-8,也適用於ASCII。可使用特殊編碼值禁用編碼NONE。若是您須要按原樣獲取從鏈接接收的字節,這主要是有用的。

請注意,寫入鏈接時,只使用定義的編碼對Unicode字符串進行編碼。字節字符串應該已經正確編碼。另請注意,測試數據中的普通文本以Unicode的形式傳遞給庫,您須要使用變量來使用字節。

若是編碼或解碼字符失敗,也能夠配置錯誤處理程序。接受的值與Python字符串接受的編碼/解碼函數相同。在實踐中,如下值是最有用的:

  • ignore:忽略沒法編碼的字符(默認)
  • strict:若是沒法對字符進行編碼,則會失敗
  • replace:替換沒法使用替換字符編碼的字符

例子:

打開鏈接 lolcathost 編碼= Latin1的 encoding_errors =嚴格
設置編碼 ISO-8859-15    
設置編碼 錯誤=忽略    

默認狀況下使用UTF-8編碼並可以配置編碼是Robot Framework 2.7.6中的新功能。在早期版本中,僅支持ASCII,而且無提示地忽略編碼錯誤。Robot Framework 2.7.7增長了指定錯誤處理程序的可能性,將默認行爲更改成忽略編碼錯誤,並添加了禁用編碼的可能性。

默認日誌級別

默認日誌級別指定用於日誌記錄的日誌級別關鍵字,除非它們具備顯式日誌級別。INFO例如,DEBUG若是存在大量沒必要要的輸出使日誌文件變大,則默認值爲,而且更改它多是一個好主意。

導入和配置Open Connection中配置默認日誌級別是Robot Framework 2.7.6中的新功能。在早期版本中,只能使用「 設置默認日誌級別」

終端類型

默認狀況下,Telnet庫不會與服務器協商任何特定的終端類型。例如vt100,若是須要特定的終端類型,則能夠在導入和使用Open Connection時配置終端類型。

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

窗口大小

導入庫和使用Open Connection時,能夠配置與服務器協商的窗口大小。

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

USER環境變量

Telnet協議容許USER在鏈接到服務器時發送環境變量。在某些服務器上可能會發生沒有登陸提示的狀況,在這種狀況下,此配置選項仍容許定義所需的用戶名。該選項environ_user可用於導入和使用Open Connection

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

終端仿真

從Robot Framework 2.8.2開始,Telnet庫支持使用Pyte進行終端仿真。終端仿真將在虛擬屏幕中處理輸出。這意味着ANSI轉義碼(如光標移動)以及控制字符(如回車符和退格鍵)對結果的影響與普通終端屏幕上的影響相同。例如,序列acdc\x1b[3Dbba將致使輸出abba

經過在庫初始化或Open Connection中terminal_emulation參數賦予真值(參見布爾參數),可使用終端仿真。

因爲Pyte近似於vt-style終端,您可能還但願將終端類型設置爲vt100。咱們還建議您增長窗口大小,由於終端仿真會破壞比窗口行長度更長的全部行。

使用終端仿真時,打開鏈接後不能再更改換行符編碼

例子:

打開鏈接 lolcathost terminal_emulation =真 TERMINAL_TYPE = VT100 WINDOW_SIZE = 400x100

做爲使用終端仿真的先決條件,您須要安裝Pyte。因爲Pyte中的向後不兼容的更改,不一樣的Robot Framework版本支持不一樣的Pyte版本:

  • Robot Framework 3.0.3支持Pyte 0.6和更新版本。能夠安裝(或升級)最新的Pyte版本pip install --upgrade pyte
  • Robot Framework 3.0.2及更早版本支持Pyte 0.5.2及更早版本。Pyte 0.5.2能夠安裝pip install pyte==0.5.2

記錄

全部讀取內容的關鍵字都會記錄輸出。這些關鍵字將日誌級別用做可選參數,若是未指定日誌級別,則使用配置的默認值。

使用有效的日誌級別TRACEDEBUGINFO(默認值),和WARNINFO默認狀況下,下面的級別不會顯示在日誌文件中,而警告會更突出地顯示。

此庫使用的telnetlib模塊具備自定義日誌記錄系統,用於記錄它發送和接收的內容。默認狀況下,這些消息是使用TRACElevel 編寫的。從Robot Framework 2.8.7開始,telnetlib_log_level可使用庫初始化,Open Connection或使用Set Telnetlib Log Level關鍵字選項來配置級別到活動鏈接。特殊級別NONE可用於徹底禁用日誌記錄。

時間字符串格式

使用的超時和其餘時間必須使用15 seconds或等格式做爲時間字符串1min 10s。若是超時被給定爲只是一個數字,例如,10或者1.5,它被認爲是秒。時間字符串格式在Robot Framework用戶指南的附錄中有更詳細的描述。

布爾參數

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

真實的例子:

打開鏈接 lolcathost terminal_emulation =真 #字符串一般是正確的。
打開鏈接 lolcathost terminal_emulation = YES #與上述相同。
打開鏈接 lolcathost terminal_emulation = $ {TRUE} #Pcthon True是真的。
打開鏈接 lolcathost terminal_emulation = $ {42} #0之外的數字爲真。

錯誤的例子:

打開鏈接 lolcathost terminal_emulation =假 #String false爲false。
打開鏈接 lolcathost terminal_emulation =無 #字符串no也是false。
打開鏈接 lolcathost terminal_emulation = $ {EMPTY} #Empty字符串爲false。
打開鏈接 lolcathost terminal_emulation = $ {FALSE} #Python False是假的。

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

輸入

參數 文檔
timeout = 3秒, newline = CRLF, prompt = None, prompt_is_regexp = False, encoding = UTF-8, encoding_errors = ignore, default_log_level = INFO, window_size = None, environ_user = None, terminal_emulation = False, terminal_type = None, telnetlib_log_level = TRACE, connection_timeout =無

可使用可選配置參數導入Telnet庫。

使用Open Connection關鍵字打開新鏈接時,配置參數將用做默認值。使用Set ... 關鍵字打開鏈接後,也能夠覆蓋它們。有關這些參數及其可能值的詳細信息,請參閱上面的這些關鍵字以及上面的配置終端仿真日誌記錄部分。

有關使用分別接受時間和布爾值的參數的信息,請參閱時間字符串格式布爾參數部分。

示例(僅使用其中一個):

設置 評論
圖書館 遠程登陸       # 默認值
圖書館 遠程登陸 5秒     #set only timeout
圖書館 遠程登陸 換行符= LF 編碼= ISO-8859-1   #使用命名參數設置換行符和編碼
圖書館 遠程登陸 提示= $     #set提示
圖書館 遠程登陸 prompt =(> |#) prompt_is_regexp = YES   #set prompt做爲正則表達式
圖書館 遠程登陸 terminal_emulation =真 TERMINAL_TYPE = VT100 WINDOW_SIZE = 400x100 #使用具備已定義窗口大小和終端類型的終端仿真
圖書館 遠程登陸 telnetlib_log_level = NONE     #禁用來自底層telnetlib的日誌記錄消息

快捷鍵

關閉全部鏈接 · 關閉鏈接 · 執行命令 · 登陸 · 打開鏈接 · 讀取 · 讀取直到 · 讀取直到提示 · 讀取直到正則表達式 · 設置默認日誌級別 · 設置編碼 · 設置換行符 · 設置提示 · 設置Telnetlib日誌級別 · 設置超時 · 交換機鏈接 · 寫入 · 寫入裸 · 寫入控制字符 ·寫入直到預期輸出

關鍵詞

關鍵詞 參數 文檔
關閉全部鏈接  

關閉全部打開的鏈接並清空鏈接緩存。

若是打開了多個鏈接,則應在測試或套件拆解中使用此關鍵字,以確保關閉全部鏈接。一些鏈接已被Close Connection關閉,這不是錯誤。

在此關鍵字以後,Open Connection關鍵字返回的新索引將重置爲1。

關閉鏈接 記錄等級=無

關閉當前的Telnet鏈接。

讀取,記錄和返回鏈接中的剩餘輸出。關閉已經關閉的鏈接並非錯誤。

若是要確保關閉全部打開的鏈接,請使用「 關閉全部鏈接」

有關日誌級別的詳細信息,請參閱日誌記錄部分。

執行命令 command, loglevel = Nonestrip_prompt = False

執行給定command和讀取,日誌,並返回全部內容,直到提示。

此關鍵字要求在導入或使用「 打開鏈接」或「 設置提示」關鍵字時配置提示

這是一個內部使用WriteRead Until Prompt的便捷關鍵字。如下兩個例子在功能上是相同的:

$ {out} = 執行命令 PWD
PWD
$ {out} = 讀取直到提示

有關參數的詳細信息,請參閱日誌記錄部分以獲取有關日誌級別和讀取提示的詳細信息strip_prompt

登陸 用戶名, 密碼login_prompt =登陸名:password_prompt =密碼:, login_timeout = 1秒login_incorrect =登陸不正確

使用給定的用戶信息登陸Telnet服務器。

此關鍵字從鏈接讀取,直到login_prompt遇到,而後鍵入給定的username。而後它讀取,直到password_prompt並鍵入給定的password。在這兩種狀況下,都會自動附加換行符,並在等待輸出時使用特定於鏈接的超時。

如何驗證日誌記錄狀態取決因而否爲此鏈接設置了提示:

1)若是設置了提示,則此關鍵字將讀取輸出,直到使用正常超時找到提示。若是未找到提示,則認爲登陸失敗,此關鍵字也失敗。請注意,在這種狀況下login_timeoutlogin_incorrect將忽略兩個參數和參數。

2)若是未設置提示,則此關鍵字將休眠login_timeout,而後讀取鏈接上可用的全部輸出。若是輸出包含login_incorrect文本,則認爲登陸失敗,而且此關鍵字也失敗。這兩個配置參數都添加到Robot Framework 2.7.6中。在早期版本中,它們是硬編碼的。

有關設置換行符,超時和提示的詳細信息,請參閱「 配置」部分。

打開鏈接 host, alias = Noneport = 23, timeout = None, newline = None, prompt = Noneprompt_is_regexp = False, encoding = Noneencoding_errors = Nonedefault_log_level = None, window_size = Noneenviron_user = Noneterminal_emulation = None, terminal_type = Nonetelnetlib_log_level = Noneconnection_timeout = None

打開與給定主機和端口的新Telnet鏈接。

timeoutnewlinepromptprompt_is_regexpencodingdefault_log_levelwindow_sizeenviron_userterminal_emulationterminal_typetelnetlib_log_level在庫參數得到默認值進口。在此處設置它們會覆蓋已打開鏈接的值。有關這些參數及其可能值的更多信息,請參閱配置終端仿真日誌記錄部分。

緩存可能已打開的鏈接,可使用Switch Connection關鍵字切換回它們。可使用顯式給定alias或使用此關鍵字返回的索引進行切換。索引從1開始,並經過Close All Connections關鍵字重置爲它。

記錄等級=無

讀取輸出中當前可用的全部內容。

讀取輸出既返回又記錄。有關日誌級別的詳細信息,請參閱日誌記錄部分。

讀直到 預期, loglevel =無

讀取輸出直到expected遇到文本。

返回並記錄包括匹配在內的文本。若是未找到匹配項,則此關鍵字將失敗。等待輸出的程度取決於配置的超時

有關日誌級別的詳細信息,請參閱日誌記錄部分。若是須要更復雜的匹配,請使用Read Until Regexp

讀取直到提示 loglevel = Nonestrip_prompt = False

讀取輸出直到遇到提示。

此關鍵字要求在導入或使用「 打開鏈接」或「 設置提示」關鍵字時配置提示

默認狀況下,返回並記錄最多包含提示的文本。若是未找到提示,則此關鍵字將失敗。等待輸出的程度取決於配置的超時

若是要從返回的輸出中排除提示,請設置strip_prompt爲true值(請參閱布爾參數)。若是提示符是正則表達式,請確保表達式跨越整個提示符,由於只刪除與正則表達式匹配的輸出部分。

有關日誌級別的詳細信息,請參閱日誌記錄部分。

可選的剝離提示是Robot Framework 2.8.7中的一項新功能。

閱讀直到Regexp *預期

讀取輸出,直到任何expected正則表達式匹配。

此關鍵字接受任意數量的正則表達式模式或編譯的Python正則表達式對象做爲參數。返回並記錄直到幷包括與任何正則表達式的第一個匹配的文本。若是未找到匹配項,則此關鍵字將失敗。等待輸出的程度取決於配置的超時

若是最後給定的參數是有效的日誌級別,則使用loglevelRead Until關鍵字相似的方式。

有關支持的正則表達式語法的更多信息,請參閱Python re模塊的文檔。請注意,可能須要在Robot Framework測試數據中轉義可能的反斜槓。

例子:

閱讀直到Regexp (#| $)  
閱讀直到Regexp first_regexp second_regexp
閱讀直到Regexp \\ d {4} - \\ d {2} - \\ d {2} DEBUG
設置默認日誌級別 level

Sets the default log level used for logging in the current connection.

The old default log level is returned and can be used to restore the log level later.

See Configuration section for more information about global and connection specific configuration.

Set Encoding encoding=None,errors=None

Sets the encoding to use for writing and reading in the current connection.

The given encoding specifies the encoding to use when written/read text is encoded/decoded, and errors specifies the error handler to use if encoding/decoding fails. Either of these can be omitted and in that case the old value is not affected. Use string NONE to disable encoding altogether.

See Configuration section for more information about encoding and error handlers, as well as global and connection specific configuration in general.

The old values are returned and can be used to restore the encoding and the error handler later. See Set Prompt for a similar example.

If terminal emulation is used, the encoding can not be changed on an open connection.

Setting encoding in general is a new feature in Robot Framework 2.7.6. Specifying the error handler and disabling encoding were added in 2.7.7.

Set Newline newline

Sets the newline used by Write keyword in the current connection.

The old newline is returned and can be used to restore the newline later. See Set Timeout for a similar example.

若是使用終端仿真,則沒法在打開的鏈接上更改換行符。

有關全局和鏈接特定配置的詳細信息,請參閱配置部分。

設置提示 promptprompt_is_regexp = False

設置當前鏈接中「 讀取直到提示」和「 登陸」使用的提示

若是prompt_is_regexp給出一個真值(參見布爾參數),則給定的prompt被認爲是正則表達式。

返回舊提示,稍後可用於恢復提示。

例:

$ {}提示 $ {regexp} = 設置提示 $
作一點事      
設置提示 $ {}提示 $ {}正則表達式  

有關支持的正則表達式語法的更多信息,請參閱Python re模塊的文檔。請注意,可能須要在Robot Framework測試數據中轉義可能的反斜槓。

有關全局和鏈接特定配置的詳細信息,請參閱配置部分。

設置Telnetlib日誌級別 水平

設置用於登陸基礎的日誌級別telnetlib

請注意,telnetlib可能會很是嘈雜,所以使用該級別NONE能夠關閉此庫生成的消息。

Robot Framework 2.8.7中的新功能。

設置超時 時間到

設置當前鏈接中等待輸出的超時時間。

指望某些輸出出現的讀取操做(Read UntilRead Until RegexpRead until PromptLogin)使用此超時,若是在超時到期以前未出現預期輸出,則會失敗。

timeout必須給予時間字符串格式。返回舊的超時,可用於稍後恢復超時。

例:

$ {old} = 設置超時 2分30秒
作一點事    
設置超時 $ {}老  

有關全局和鏈接特定配置的詳細信息,請參閱配置部分。

開關鏈接 index_or_alias

使用索引或別名在活動鏈接之間切換。

別名能夠給予Open Connection關鍵字,該關鍵字也始終返回鏈接索引。

此關鍵字返回先前活動鏈接的索引。

例:

打開鏈接 myhost.net    
登陸 約翰 祕密  
一些命令    
打開鏈接 yourhost.com 第二個康涅狄格州  
登陸 密碼  
另外一個cmd    
$ {old index} = 開關鏈接 1 #index
某物    
開關鏈接 第二個康涅狄格州   #alias
隨你    
開關鏈接 $ {舊索引}   #回到原創
[拆除] 關閉全部鏈接    

上面的示例指望在打開第一個鏈接時沒有其餘打開的鏈接,由於它1在之後切換到鏈接時使用了索引。若是您不肯定,能夠將索引存儲到變量中,以下所示。

$ {index} = 打開鏈接 myhost.net
作一點事    
開關鏈接 $ {}指數  
text, loglevel =無

將給定文本和換行符寫入鏈接。

要使用的換行符能夠在全局和每一個鏈接的基礎上配置。默認值爲CRLF

此關鍵字從輸出和日誌中消耗書面文本,直到添加的換行符並返回它。給定的文本自己不得包含換行符。若是這些功能中的任何一個致使問題,請使用Write Bare

注意:此關鍵字不會返回已執行命令的可能輸出。要得到輸出,必須使用其中一個Read ... 關鍵字。有關詳細信息,請參閱寫入和閱讀部分。

有關日誌級別的詳細信息,請參閱日誌記錄部分。

寫裸 文本

將給定的文本寫入鏈接,而不是其餘任何內容。

此關鍵字不會附加換行符,也不會使用書面文本。若是須要這些功能,請使用Write

寫控制字符 字符

將給定的控制字符寫入鏈接。

控制字符前面加上IAC(解釋爲命令)字符。

支持如下控制字符名稱:BRK,IP,AO,AYT,EC,EL,NOP。此外,您可使用任意數字發送任何控制字符。

例:

寫控制字符 BRK #Send Break命令
寫控制字符 241 #Send no operation命令
寫入直到預期輸出 text, expectedtimeoutretry_intervalloglevel = None

text反覆寫入給定,直到expected出如今輸出中。

text在沒有附加換行符的狀況下寫入,並在嘗試查找以前從輸出中消耗掉換行符expected。若是expected未在輸出中出現,則timeout此關鍵字失敗。

retry_interval定義expectedtext再次寫入以前等待出現的時間。使用寫入text是受正常配置的超時限制

雙方timeoutretry_interval必須給予時間字符串格式。有關日誌級別的詳細信息,請參閱日誌記錄部分。

例:

寫入直到預期輸出 ps -ef | grep myprocess \ r \ n myprocess
... 5秒 0.5秒

上面的示例寫入命令ps -ef | grep myprocess\r\n直到myprocess輸出中出現。該命令每0.5秒寫入一次,若是myprocess輸出在5秒內沒有出現,則關鍵字將失敗。

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

相關文章
相關標籤/搜索