RobotFramework之Telnet

原创
2018/08/15 09:36
阅读数 2K

Telnet

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

Introduction

A test library providing communication over Telnet connections.

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

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.

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.

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.

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.

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 生成。

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部