1

我正在尝试根据传递给脚本的参数打开浏览器 url。因此,我编写了以下 ruby​​ 代码:

require 'selenium-webdriver'
require 'test/unit'

class TestTitle < Test::Unit::TestCase
  def setup
    $driver = Selenium::WebDriver.for :firefox
    if ARGV[0] == 'google'
      $driver.get 'http://www.google.com'
    elsif ARGV[0] == 'twitter'
      $driver.get 'http://www.twitter.com'
    end
  end

  def test_title
    puts $driver.title
  end

  def teardown
    $driver.quit
  end
end

当我传递参数:ruby test.rb 'google'时,会导致以下错误:

c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: google (ArgumentError)
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
        from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
        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'

请帮助我理解我做错了什么。

4

1 回答 1

3

似乎测试单元(从 1.9.1 开始)在其 GlobOptions 模块中获取命令行选项。您正在使用 ARGV[0] 传递浏览器名称,但它认为您正在传递文件名。一种解决方法是捕获 ARGV[0] 的值,然后在测试用例运行之前将其清除:

browser = ARGV[0]

ARGV[0] = nil

于 2013-12-10T03:16:52.033 回答