expect4groovy

Printer-friendly versionPDF version

Expect4Groovy is a library allowing groovy developers to expect

Expect is a tool for automating interactive applications such as telnet, ssh and other interactive CLI applications.
Expect4Groovy really makes this stuff trivial for Groovy developers and also for java developers.
The benefit over the existing ExpectJ and Expect4J and other Expect-for-Java projects is that it supports nested java closures.
The nested closures allow the developer to expect nested statements.
Expect4Groovy is distributed under Apache 2 Open Source license.
 

groovy closures

The above Groovy closures are registered into script bindings with one of the following overloads of createBindings method:

void Expect4Groovy.createBindings(CLIConnection cliConnection, Binding binding, boolean withLogging);
Map<String, Object> Expect4Groovy.createBindings(CLIConnection cliConnection);
Map<String, Object> Expect4Groovy.createBindings(InputStream is, OutputStream os);

Example:

CLIConnection conn = new RawSocketCLIConnection()
conn.connect(["user":"v","password":"123","address":"localhost:23"])
Expect4Groovy.createBindings(conn, getBinding(), true)

Another available connections are:

  • net.itransformers.expect4groovy.cliconnection.impl.SshCLIConnection
  • net.itransformers.expect4groovy.cliconnection.impl.EchoCLIConnection
  • net.itransformers.expect4groovy.cliconnection.impl.RawSocketCLIConnection
  • net.itransformers.expect4groovy.cliconnection.impl.TelnetCLIConnection

Example expect4groovy script

import net.itransformers.expect4groovy.Expect4Groovy
import net.itransformers.expect4groovy.cliconnection.CLIConnection
import net.itransformers.expect4groovy.cliconnection.impl.EchoCLIConnection
import net.itransformers.expect4java.ExpectContext
import org.apache.log4j.Level
import org.apache.log4j.LogManager

if (args.length == 0){ LogManager.getRootLogger().setLevel(Level.INFO); } else { LogManager.getRootLogger().setLevel(Level.toLevel(args[0])); }

CLIConnection conn = new EchoCLIConnection() def params = [:] // empty map for echo connection conn.connect(params)

Expect4Groovy.createBindings(conn, getBinding(), true)

expect.setTimeout(1000){
println "Timeout while expecting"
}

// simple send to echo connection send("echo\n") send("test\n") expect ("echo\n") { // Example how to use nested expect closures expect("test\n") { send("hello\n") // send to echo connection again } } // expect hello as it should be send already to the echo connection expect("hello\n");

// Example usage of '_re' closure send ("echo1234") expect (_re("[a-z]+([0-9]+)"){ println("Captured: "+it.getMatch(1)) })

// More complicated examples with array of Match closures. // Shows also how to use exp_continue of ExpectContext send ("echo1234\n") send ("5678echo\n") send ("ZZZ\n") expect ([ _re("[a-z]+([0-9]+)"){ ExpectContext it -> println("Captured: "+it.getMatch(1)) it.exp_continue(); }, _re("([0-9]+)[a-z]+"){ println("Captured: "+it.getMatch()) it.exp_continue(); }, _gl("ZZZ"){ println("Captured: ZZZ") } ])

//Shows how the global timeout closure will be invoked. send("hello\n") expect("John"){ println("This text should not appear in console") }

//Shows how the local timeout closure will be invoked. send("test\n") expect([ _gl("Smith") { println("This text should not appear in console") }, _timeout(500){ println("This is a timeout example") } ])

// Lets close echo connection conn.disconnect()

// Even the connection is closed the eof will be in the expect buffer // so lets expect for eof (end of file). expect _eof()

Dependencies

expect4groovy runtime depends on the following other libraries.

  • Maven: org.codehaus.groovy:groovy-all:2.1.9
  • Maven: org.apache.commons:commons-net:3.3.1
  • Maven: com.jcraft:jsch:0.1.44-1 (optional for ssh connections)
  • Maven: commons-net:commons-net:3.0.1
  • Maven: oro:oro:2.0.8
  • Maven: log4j:log4j:1.2.16
  •