After struggliing with running Selenium with Cruisecontrol I hit on a new solution today and am now able to run Selenium and post results more consistently than before. This way of doing it has the advantages that I did not need to run a rails app to collect results. I have managed to do it with Webrick. Since it was my first time working with Webrick it took me lot longer than I thought.

To start with, here is the problem I was facing. I was trying to do the Cruisecontrol with Selenium all over again on a fresh setup. Once I installed Cruisecontrol and Java(u have to have Java - I had ruby already). I was ready to run the same batch file I had. But I was not able to post the results. Installing a rails application with all the required gems seemed painful and an overkill for the purpose. So I started poking around with WEBrick. Some documentation on WEBrick website got me started and I could send a request to the server. But what I was not able to do was read the request parameters. All the documentation I looked up talked about the same kinds of examples as on the WEBrick website. They all mentioned and showed with an example how to handle simple requests and responses. Printing out the request did not help as it did not have parameters anywhere. On further digging I found some code in the PickAxe book (edition 2 page 248)

where the example spelled out exactly where the query object was 'req.query'. That was where one will find the parameters hash that Selenium populates when it posts results.

Here is a reference of the code that achieved the desired result.

require 'webrick'

if ARGV.empty?
p "Usage: ruby post_results.rb browser_name"
p "Example: ruby post_results.rb iexplore"

@browser = ARGV[0]
resultsURL= "http://localhost:2000/postResults" #the server that gathers the results..
command = "#{@browser} \"#{testRunner}\""

system "start #{command}" #DOS batch command start starts the command in a new thread

include WEBrick

s = :Port => 2000 )

# HTTPServer#mount_proc(path){req, res ...}
# You can mount also a block by `mount_proc'.
# This block is called when GET or POST.
def save_to_file(params)
Dir.mkdir("results") unless FileTest.exist?("results")
Dir.mkdir(@browser) unless FileTest.exist?(@browser)
results_file_name = "selenium-results.html"

save to file as before


s.mount_proc("/postResults"){|req, res|
params = req.query
results_file_name = save_to_file(params)
system "TASKKILL /F /IM #{@browser}.exe" #KILL the browser since selenium is done.

trap("INT"){ s.shutdown }


Marshall Mania said...

nice dude....