我正在尝试修复我的测试,因此我可以使用环境变量从命令行调用它们,例如浏览器。
我在命令提示符下调用它: ruby watir1_environment.rb -BROWSER = chrome
并收到以下错误消息:
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'
这是代码:
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
任何想法?
答案 0 :(得分:2)
您可以使用set
命令从命令行设置Windows ENV变量。
C:\> SET BROWSER=chrome
C:\> ruby watir1_environment.rb
您可以在http://ss64.com/nt/set.html找到有关set
的更多信息。
或者,您可以访问ARGV
变量,而不是使用ENV['BROWSER']
,这是在命令行传递的参数数组。只需将您的案例陈述更改为:
case ARGV[0]
这只需要关键字,而不是-BROWSER=
。
C:\> ruby watir1_environment.rb chrome
答案 1 :(得分:0)