A test library providing Telnet connections and communicating with them.
This library takes the optional arguments timeout, newline, regexp and
regexp_is_prompt. These are default values used when a new connection is
opened with the keyword 'Open', and they can be overridden. The purpose of
these parameters can be found from the documentation of 'Open'.
Examples (use only one of these):
Keyword |
Arguments |
Documentation |
Close All Connections |
|
Closes all open connections and empties the connection cache.
After this keyword, new indexes got from the 'Open' keyword are reset
to 1.
This keyword should be used in a test or suite teardown to make sure
all connections are closed. |
Close Connection |
loglevel=None |
Closes the current Telnet connection and returns any remaining output.
See 'Read' for more information on 'loglevel'. |
Login |
username, password, login_prompt=login: , password_prompt=Password: |
Logs in to the Telnet server with the given user information.
The login keyword reads from the connection until login_prompt is
encountered and then types the username. Then it reads until
password_prompt is encountered and types the password. The rest of the
output (if any) is also read, and all the text that has been read is
returned as a single string.
If a prompt has been set to this connection, either with 'Open Connection'
or 'Set Prompt', this keyword reads the output until the prompt is
found. Otherwise, the keyword sleeps for a second and reads everything
that is available. |
Open Connection |
host, alias=None, port=23, timeout=None, newline=None, prompt=None, prompt_is_regexp=False |
Opens a new Telnet connection to the given host and port.
Possible already opened connections are cached.
Returns the index of this connection, which can be used later to switch
back to the connection. The index starts from 1 and is reset back to it
when the 'Close All Connections' keyword is used.
The optional alias is a name for the connection, and it can be used for
switching between connections, similarly as the index. See 'Switch
Connection' for more details about that.
The timeout newline and prompt attributes got default values when the
library was taken into use, but they can be overridden for each opened
connection and also set after opening the connection using the
'Set Timeout', 'Set Newline' and 'Set Prompt' keywords.
Timeout is used with keywords that start 'Read Until'. If the text they
are searching is not found within the timeout, the keywords fail.
Newline is the newline character in the target system.
Prompt is the prompt character of the taget system, and it can be given
as a regular expression when promp_is_regexp is set to True. |
Read |
loglevel=None |
Reads and returns/logs everything that is currently available in the output.
The read message is always returned and logged. The default log level is
either 'INFO', or the level set with 'Set Default Log Level'.
'loglevel' can be used to override the default log level, and the
available levels are TRACE, DEBUG, INFO, and WARN. |
Read Until |
expected, loglevel=None |
Reads from the current output, until expected is encountered.
Text up to and including the match is returned. If no match is
found, the keyword fails.
See 'Read' for more information on 'loglevel'. |
Read Until Prompt |
loglevel=None |
Reads from the current output, until a prompt is found.
The prompt must have been set, either in the Library import or at login
time, or by using the 'Set Prompt' keyword.
See 'Read' for more information on 'loglevel'. |
Read Until Regexp |
*expected |
Reads from the current output, until a match to a regexp in expected.
Expected is a list of regular expressions, and keyword returns the text
up to and including the first match to any of the regular
expressions.
If the last argument in *excpected is a valid log level, it is used
as 'loglevel' in the keyword 'Read'.
Examples:
Read Until Regexp |
(#|$) |
|
Read Until Regexp |
first_regexp |
second_regexp |
Read Until Regexp |
some regexp |
DEBUG |
|
Set Default Log Level |
level |
Sets the default log level used by all read keywords.
The possible values are TRACE, DEBUG, INFO and WARN. The default is
INFO. The old value is returned and can be used to restore it later,
similarly as with 'Set Timeout'. |
Set Newline |
newline |
Sets the newline used by the 'Write' keyword.
The old newline is returned and can be used to restore it later.
See 'Set Timeout' for an example. |
Set Prompt |
prompt, prompt_is_regexp=False |
Sets the prompt used in this connection to 'prompt'.
If 'prompt_is_regexp' is a non-empty string, the given prompt is
considered to be a regular expression.
The old prompt is returned and can be used to restore it later.
Example:
${prompt} |
${regexp} = |
Set Prompt |
$ |
Do Something |
|
|
|
Set Prompt |
${prompt} |
${regexp} |
|
|
Set Timeout |
timeout |
Sets the timeout used in read operations to the given value.
'timeout' is given in Robot Framework's time format
(e.g. 1 minute 20 seconds).
The read operations wait for this time before starting to read
from the output. To run operations that take a long time to generate
their complete output, this timeout must be set accordingly.
The old timeout is returned and can be used to restore it later.
Example:
${tout} = |
Set Timeout |
2 minute 30 seconds |
Do Something |
|
|
Set Timeout |
${tout} |
|
|
Switch Connection |
index_or_alias |
Switches between active connections using an index or alias.
The index is got from Open keywords, and an alias can be given to it.
Example:
Open |
myhost.net |
|
Login |
john |
secret |
Write |
some command |
|
Open |
yourhost.com |
2nd conn |
Login |
root |
password |
Write |
another cmd |
|
Switch Connection |
1 |
# index |
Write |
something |
|
Switch Connection |
2nd conn |
# alias |
Write |
whatever |
|
[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.
${id} = |
Open |
myhost.net |
# Do something ... |
|
|
Switch Connection |
${id} |
|
|
Write |
text, loglevel=None |
Writes the given text over the connection and appends a newline.
Consumes the written text (until the appended newline) from the output
and returns it. The given text must not contain newlines.
Note: This keyword does not return the possible output of the executed
command. To get the output, one of the 'Read XXX' keywords must be
used. |
Write Bare |
text |
Writes the given text over the connection without appending a newline.
Does not consume the written text. |
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. 'retry_interval' defines
the time waited before writing 'text' again. 'text' is consumed
from the output before 'expected' is tried to be read.
If 'expected' does not appear in the output within 'timeout', this
keyword fails.
See 'Read' for more information on 'loglevel'.
Example:
Write Until Expected Output |
ps -ef| grep myprocess\n |
myprocess |
... |
5s |
0.5s |
This writes the 'ps -ef | grep myprocess\n', until 'myprocess'
appears on 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.
New in Robot Framework version 1.8.2. |