11
Expect Tool for Automation and Testing

Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Embed Size (px)

Citation preview

Page 1: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect

Tool for Automation and Testing

Page 2: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect - Introduction

Unix automation and testing tool

Written by Don Libes Primarily designed as an

extension to the Tcl scripting language

Useable in interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others.

Page 3: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect – TCL* Basics

Everything is a command No Fixed Parser Case-Sensitive Explicit Expression Evaluation

– set a 5 a = 5– set b a+2 b = a+2– set c [expr $a+2] c = 7 (Note: $ for variable substitution)

Simple variables are strings of arbitrary length Real & Int cast to real Non-numeric string and real/int cast to string Built in Lists Standard flow control structures (while, if, etc.) String manipulation with Regular Expressions Procedures – arguments can be passed by value or reference*Tool Command Language

Page 4: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect – The Programming Language

Extension of TCL, so the syntax is TCL syntax Basic command set

– expect– send– spawn– Interact

Regular Expression pattern matching “Glue” to combine separate executables

Page 5: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect – Pros / Cons

Pros– Built on existing system tools, so learning

curve is small.– Extensive support from corporate entities*

*Silicon Graphics, IBM, HP, Sun, Xerox, Amdahl, Tektronix, AT&T, ComputerVision and the World Bank

– Numerous ports to other programming languages (Perl, Python, java, etc.)

Cons– Only useful for command line scripting– Cryptic syntax for those unfamiliar with TCL

(When do I use a $ again?)– Generally machine dependent tools are run

with Expect, so porting scripts cross-platform is not easily supported.

Page 6: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect – “Hello World!”

Start Expect with “expect” or write the script to a file and run > expect filename

send “Hello World!\n” expect “Hi\n”Putting it together Expect “Hi\n” { send “Hi World!\n” }

Page 7: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect – More Advanced

expect "hi\n" { send "Hi World!\n" } \

"hello\n" {send "Hello World!\n" } \

"bye\n" {send "Bye World!\n" }

Page 8: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect - Automation

spawn ftp $argvexpect "Name" { send "anonymous\r" } \ "name" { send "anonymous\r" }expect "assword:"send "[email protected]\r"interact

Page 9: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect - Others

Brute Force security Beer Chess Weather Rogue Hunt

Page 10: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect - GnuChess

# start things rollingspawn gnuchessset id1 $spawn_idexpect "White \\(1\\) :"send "random\r"send "depth 6\r"send "go\r"# read_first_moveexpect -re "My move is : (.*)\n"

spawn gnuchessset id2 $spawn_idexpect "White \\(1\\) :"send "random\r"send "depth 5\r"send $expect_out(1,string)

while {1} { expect { -i $id2 -re "is : (.*)\n" { send -i $id1 $expect_out(1,string) } -i $id1 -re "is : (.*)\n" { send -i $id2 $expect_out(1,string) } }}

Page 11: Expect Tool for Automation and Testing. Expect - Introduction Unix automation and testing tool Unix Written by Don LibesDon Libes Primarily designed as

Expect - Questions