Question

I'm trying to fix my tests so I can call them from the command line with enviromental variables, like what browser.

I call it in a command prompt: ruby watir1_environment.rb -BROWSER=chrome

And get the following error message:

C:/Ruby193/lib/ruby/1.9.1/test/unit.rb:49:in `process_args': invalid option: -BROWSER=chrome (OptionParser::InvalidOption)
        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'

This is the code:

require "rubygems"
require "test/unit"
require "watir-webdriver"


class TestingEnvironments < Test::Unit::TestCase
def setup
case ENV['BROWSER']
  when 'ff', 'Firefox'
  @b = Selenium::WebDriver.for :firefox
  browser_name = 'Firefox'
when 'chrome'
  @b = Selenium::WebDriver.for :chrome
  browser_name = 'Chrome'
when 'debug'
  debug_profile = Selenium::WebDriver::Firefox::Profile.new
  debug_profile.add_extension "firebug-1.9.1-fx.xpi"
  @b = Selenium::WebDriver.for :firefox, :profile => debug_profile
  browser_name = 'Firefox (Firebug)'
when 'mobile'
  mobile_profile = Selenium::WebDriver::Firefox::Profile.new
  mobile_profile['general.useragent.override'] = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)
  AppleWebKit/420+ (KHTML, like Gecko) Version/3.0
  Mobile/1A535b Safari/419.3"
  @b = Selenium::WebDriver.for :firefox, :profile => mobile_profile
  browser_name = 'Mobile'
when 'headless'
  headless_profile = Headless.new
  headless_profile.start
  @b = Selenium::WebDriver.for :firefox
  browser_name = 'Firefox'
else
 @b = Selenium::WebDriver.for :firefox
 browser_name = 'Firefox'
end
end

def test
  {Test Code}
end

Any Ideas?

Was it helpful?

Solution

You can set Windows ENV variables from the command line by using the set command.

C:\> SET BROWSER=chrome
C:\> ruby watir1_environment.rb

You can find out more about set at http://ss64.com/nt/set.html.

Alternatively, rather than using ENV['BROWSER'] you can access the ARGV variable, which is an array of arguments passed at the command line. Just change your case statement to:

case ARGV[0]

This requires only the keyword, and not the -BROWSER=.

C:\> ruby watir1_environment.rb chrome

OTHER TIPS

I pass the variables in without the dash and it works fine.

ruby watir1_environment.rb BROWSER=chrome

I also use the Fig_Newton gem for variable's that I use regularly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top