Search code examples
rubyunit-testingseleniumoptionparser

optparse in ruby test to set target server for webdriver


I need to have my webdriver tests hit multiple servers. My solution was to pass in a command line parameter but when run the code below using

ruby webdriver/e2e/*.rb -s=localhost

or

ruby webdriver/e2e/*.rb --server=localhost

but I get the following error. I get the following error

C:/Ruby193/lib/ruby/1.9.1/test/unit.rb:49:in `process_args': invalid argument: -s=localhost (OptionParser::InvalidArgument)
    from C:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
    from C:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
    from C:/Ruby193/lib/ruby/1.9.1/test/unit.rb:21:in `run'
    from C:/Ruby193/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
    from C:/Ruby193/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
    from C:/Ruby193/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'

test.rb

require 'test/unit'
require 'optparse'

class SuperTestClass < Test::Unit::TestCase
    def setup 
         @server = "https://localhost/"
         optparse = OptionParser.new do|opts|            
             opts.on( '-s', '--server server', 'Server to run tests against' ) do|server|
                 @server = server
             end
         end

        ...
    end
    ...
end

UPDATE

I changed

opts.on( '-s', '--server server', 'Server to run tests against' ) do|server|

to

  opts.on( '-serv', '--server server', 'Server to run tests against' ) do|server|

but it didn't fix it


Solution

  • I don't think tests can take parameters. I ended up using rake to set env paramters.

    task :selenium,[:env,:type] do |t, args|
        args.with_defaults  :env => 'local',:type => 'all'
    
        case args.env
            when 'local'
                ENV['URL'] = 'localhost'
                ...
        end
    
        puts
        puts '****Running '+args.type+' tests on: '+ ENV['URL']
        puts
    
        Rake::Task[args.type].execute
    end