Installing the Watir Gem under Windows 7 Finding Office Application Version numbers using Ruby
Jan 17

Lately I’ve been using Behavioral-Driven Design (BDD) principals in developing custom software for my customers. As part of that, I have been using the RSpec gem in Ruby to test my developing software. The ’spec’ program has a very nice HTML output option that will show in a hurry if my tests passed or not:
RSpec output
To create this output though, I had to run the program, then try and open the output file in my web browser. Thinking there was a better way to do this, I created the following program:

#simple RSpec web server

require 'webrick'
include WEBrick

class RunSpec < HTTPServlet::AbstractServlet

   def do_GET(req, res)
     ## Looking for a get of "/runspec?spec=this_spec"
     puts "REQ>#{req.request_line}"
     spec = req.request_line[/\?spec=(.*) HT/, 1]
     puts "SPEC>#{spec}\n"
     res.status = 200
     if File.exists?("#{spec}.rb")
       system "spec -f h #{spec}.rb > #{spec}.html"
       ff ="#{spec}.html").read
       res.body = ff
       res['Content-Type'] = 'text/html'
       if spec == nil
         rr = <<-EOT
<HTML><HEAD><TITLE>RunSpec Web Server</TITLE></HEAD><BODY><H2>RunSpec Web Server</h2>
This server runs the specified spec file and returns the results as a web page.<br><br>
You specify the spec file like so:<br>http://localhost:9099/runspec?spec={your_spec without any extension}<br>
The program assumes a '.rb' extension.</BODY></HTML>
         res.body = rr
         res['Content-type'] = 'text/html'
         res.body = "Error! Unable to open #{spec}.rb file!"
         res['Content-Type'] = 'text/plain'

   alias :do_POST :do_GET

s = => 9099)'rhtml', 'text/html') :CGIInterpreter, "#{HTTPServlet::CGIHandler::Ruby}")

s.mount('/', HTTPServlet::FileHandler, Dir.pwd)
s.mount('/runspec', RunSpec)

['TERM', 'INT'].each do |signal|
   trap(signal){ s.shutdown }


Simply run this Ruby script in the directory where your RSpec spec file is located and it will start a web server locally on port 9099. Then just connect to ‘HTTP://localhost:9099/runspec?spec=test_spec‘ with ‘test_spec’ being your test_spec.rb filename and it will run the spec and show the output. If you just run ‘/runspec’ it will output a very simple directions page.

The script also has the benefit of being able to serve up .rhtml and other web content files from the same directory.
This has been a fun time saver for me!

SociBook Digg Facebook Google Yahoo Buzz StumbleUpon

Leave a Reply