Groovy — различия между версиями

(expect Matcher)
Строка 53: Строка 53:
 
* <code>expect sequence(contains("vasya"), contains("petya"))</code> - ожидать, пока в output появится "vasya", а затем "petya" (именно в этом порядке)
 
* <code>expect sequence(contains("vasya"), contains("petya"))</code> - ожидать, пока в output появится "vasya", а затем "petya" (именно в этом порядке)
 
* <code>expect allOf(contains("vasya"), contains("petya"))</code> - ожидать, пока в output появится "vasya" и "petya" (в любом порядке)
 
* <code>expect allOf(contains("vasya"), contains("petya"))</code> - ожидать, пока в output появится "vasya" и "petya" (в любом порядке)
 +
 +
 +
== <code>send "string"</code> ==
 +
Отослать в telnet-сессию <code>string</code> (без <code>Enter</code> в конце).
 +
 +
Примеры:
 +
* <code>send "con t"</code>
 +
* <code>send "\b\b\b\b\b"</code>
 +
* <code>send "man $port_number\n"</code>
 +
 +
 +
== <code>sendLine "string"</code> ==
 +
Отослать в telnet-сессию <code>string</code> (с <code>Enter</code> в конце).<br>
 +
То же самое, что <code>send "string\n"</code>.
 +
 +
Примеры:
 +
* <code>sendLine "con t"</code>
 +
* <code>sendLine login</code>
 +
* <code>sendLine "man $port_number"</code>
 +
 +
 +
== <code>sendLine()</code> ==
 +
Отослать <code>Enter</code>.<br>
 +
То же самое, что <code>send "\n"</code> или <code>sendLine ""</code>.
 +
 +
Примеры:
 +
* <code>sendLine()</code>

Версия 12:34, 22 апреля 2016

Общие сведения

Любые строки нужно брать в кавычки, т.к. это язык программирования. В одинарных кавычках все отправляется как есть, в двойных интерполируются имена переменных с долларом (превращаются в значения).

Одни из самых часто используемых команд:

  • SendLine "something" - отправить это в телнет-сессию и

добавить в конце <LF>.

  • Send "something" - то же самое, но без <LF> в

конце.

  • expect "something" - ожидать, пока телнет выведет в

output текст "something".

Переменные вне кавычек пишутся просто без каких-либо символов вначале. т.е. port_name, prompt и т.п.


Доступные команды:

expect "string"

Ожидать, пока telnet-сессия выведет в output string.

Примеры:

  • expect "#"
  • expect prompt
  • expect "# port_$port_number sn:$onu_sn"

expect Matcher

Ожидать, пока telnet-сессия выведет в output строку, соответствующую Matcher.

Возможные Matcher-ы:

  • allOf(Matcher, Matcher...) - Creates a matcher that matches if the examined input matches all of the specified matchers.
  • anyOf(Matcher, Matcher...) - Creates a matcher that matches if the examined input matches any of the specified matchers.
  • anyString() - Creates a matcher that matches when at least one character exists in the input buffer.
  • contains(String) - Creates a matcher of String that matches when examined input contains the given substring.
  • eof() - Creates a matcher that matches if input reaches the end of stream (closed telnet session).
  • exact(String) - Creates a matcher that matches when the given string is equal to the input buffer contents.
  • matches(String) - Creates a matcher of String that matches when examined input fully matches the given regular expression.
  • matches(Pattern) - Creates a matcher of Pattern that matches when examined input fully matches the given regular expression.
  • regexp(String) - Creates a matcher of String that matches when examined input contains the given regular expression.
  • regexp(Pattern) - Creates a matcher of Pattern that matches when examined input contains the given regular expression.
  • sequence(Matcher, Matcher...) - Matches the given matchers one by one. Every successful matches updates the internal buffer. The consequent match operation is performed after the previous match has succeeded.
  • startsWith(String) - Creates a matcher that matches when the input buffer starts with the given string.
  • times(int, Matcher) - Creates a matcher that matches if the given Matcher matches the number of times.

Примеры:

  • expect contains("vasya") - то же самое, что и expect "vasya"
  • expect regexp("# $") - ожидать, пока в output появится строка, соответствующая регулярному выражению "# $" (output будет заканчиваться на решетку и пробел)
  • expect times(3, contains("vasya_$num")) - ожидать, пока в output 3 раза встретится строка "vasya_$num" ($num будет интерполировано)
  • expect anyOf(contains("vasya"), contains("petya")) - ожидать, пока в output появится либо "vasya", либо "petya"
  • expect sequence(contains("vasya"), contains("petya")) - ожидать, пока в output появится "vasya", а затем "petya" (именно в этом порядке)
  • expect allOf(contains("vasya"), contains("petya")) - ожидать, пока в output появится "vasya" и "petya" (в любом порядке)


send "string"

Отослать в telnet-сессию string (без Enter в конце).

Примеры:

  • send "con t"
  • send "\b\b\b\b\b"
  • send "man $port_number\n"


sendLine "string"

Отослать в telnet-сессию stringEnter в конце).
То же самое, что send "string\n".

Примеры:

  • sendLine "con t"
  • sendLine login
  • sendLine "man $port_number"


sendLine()

Отослать Enter.
То же самое, что send "\n" или sendLine "".

Примеры:

  • sendLine()