2013-09-21 31 views
1

我想根據傳遞給腳本參數打開瀏覽器的URL。因此,我寫了下面的Ruby代碼:`塊non_options':找不到文件:(引發ArgumentError)

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' 

請幫助我瞭解我在做什麼錯。

+1

您是如何從命令提示符運行您的.rb文件的? –

+0

'紅寶石test.rb「google'' – TDHM

+0

你的shell命令的第二個參數被解釋爲一個文件不是一個ARGV說法。到目前爲止,Testunit甚至沒有觸及你的代碼。你正在運行什麼版本的testunit? ruby是別名嗎? – iltempo

回答

3

似乎測試單元(如1.9.1)抓住在其GlobOptions模塊的命令行選項。您正在使用ARGV [0]傳遞瀏覽器名稱,但它認爲您傳遞的是文件名。解決方法是捕獲ARGV [0]的值,然後在測試用例運行前將其清除:

browser = ARGV[0]

ARGV[0] = nil

相關問題