Did you ever use Expect to automate a complex command-line procedure, like an FTP upload? Expect is handy – and very powerful – but for Ruby projects it sure would be nice to be able to automate console apps directly from Ruby.
You may not have known it, but Ruby actually ships with a tiny Expect clone called expect.rb. Unfortunately it’s more of a proof of concept than a fully functional automation tool.
Enter Greenletters. Greenletters begins to bring some of the power of Expect to Ruby, with a simple, straightforward API. For example, here’s a scripted interaction with the classic Colossal Cave Adventure:
require 'greenletters' adv = Greenletters::Process.new("adventure", :transcript => $stdout) # Install a handler which may be triggered at any time adv.on(:output, /welcome to adventure/i) do |process, match_data| adv < < "non" end puts "Starting adventure..." adv.start! # Wait for the specified pattern before proceeding adv.wait_for(:output, /you are standing at the end of a road/i) adv << "eastn" adv.wait_for(:output, /inside a building/i) adv << "quitn" adv.wait_for(:output, /really want to quit/i) adv << "yesn" adv.wait_for(:exit) puts "Adventure has exited."
Greenletters also ships with some simple Cucumber steps, so you can immediately start using it to specify the behavior of your command-line apps:
Given process activity is logged to "greenletters.log" Given a process "adventure" from command "adventure" Given I reply "no" to output "Would you like instructions?" from process "adventure" Given I reply "yes" to output "Do you really want to quit" from process "adventure" When I execute the process "adventure" Then I should see the following output from process "adventure": """ You are standing at the end of a road before a small brick building. Around you is a forest. A small stream flows out of the building and down a gully. """ When I enter "east" into process "adventure" Then I should see the following output from process "adventure": """ You are inside a building, a well house for a large spring. """
Want to give it a try? Then:
gem install greenletters
And check out the examples/ directory for inspiration. Let me know what you think!
I tried it and my machine (ruby191) couldn't require 'pty'. What kind of dependency is that? #greenletters
pty is a Ruby standard library. What is your platform? As far as I know PTY (and thus, Greenletters) will not work under Win32.
What is the hardware in the photo?
IBM 3270.
That's an IBM 3270 terminal.
Cool tool. Thanks. Do you know an easy way to capture the output to a variable. I need to do some validation on results.