views:

718

answers:

2

I'm pretty new to both Ruby and Selenium and I'm just trying to figure out the best way to build my test harness. I'm using Rake, Rake::TestTask, and Test::Unit to power this. I have a suite that I'd like to run once for each browser/os combination. I can't figure out how to parameterize my tests though, something I've become accustomed to with Junit4 and TestNG.

require 'rake'
require 'rake/testtask'

browsers = ['IE on Windows', 'Firefox on Windows', 'Firefox on Mac', 'Safari on Mac']

task :default => [:run_tasks]

task :create_tasks do
  browsers.each do |browser|    
    Rake::TestTask.new("selenium_units:#{browser}") do |t|
      t.libs << "lib"
      t.pattern = 'test/*_test.rb'
      t.verbose = true
      t.warning = true
      t.opts = "BROWSER=\"#{browser}\""
    end
  end  
end

task :run_tasks => [:create_tasks]
task :run_tasks => browsers.map { |e| "selenium_units:"+ e }

I'd really like to be able to read that BROWSER= in the setup of my Suites or Cases. Any suggestions or is there plainly a better way of doing this in Ruby?

+1  A: 

After a lot of digging through the source I found a not so documented feature. The Test::Unit test runner doesn't do anything besides executing your test cases on the commandline relying on an autorunner to run any case cases of that class specific class. So, when they say anything after -- will be passed as options, they mean command line options, not some variable or parameter.

so... adding

t.options = '-- -foo -bar=baz'

Will actually pass those into your ARGV and you are expected to process them manually. Taking those args and throwing them into a factory class to grab the appropriate Selenium instance will work for me.

Trey
A: 

This may not be what you are asking. But I've solved the exact same problem in Python and blogged about it in Parameterizing Your Tests. I am sure you can use the same approach in Ruby, not sure how it compares with what you've done. I am interested to know how others have solved this problem though, and haven't found much documentation.

toby
Thanks for the link. Same exact problem and predictably, a similar solution. I did consider writing my own test runner similar to yours but wanted to get everything as close to the "ruby" way as possible despite the relatively immature testing library. Good write-up.
Trey