无法使用 OptionParser 和 rspec [英] Unable to use OptionParser and rspec

查看:44
本文介绍了无法使用 OptionParser 和 rspec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 watir(网络驱动程序)脚本,可用于 google.但是,我想使用选项解析器在 cmd 中设置一个参数来选择浏览器.下面是我的脚本:

I have a simple watir (web-driver) script which goes to google. But, I want to use option parser to set an argument in the cmd to select a browser. Below is my script:

require 'optparse'
require 'commandline/optionparser'
include CommandLine
require 'watir-webdriver'

describe 'Test google website' do

  before :all do

    options = {}

    opts = OptionParser.new do |opts|

      opts.on("--browser N",
        "Browser to execute test scripts") do |n|
        options[:browser] = n
        $b = n.to_s
      end
    end

    opts.parse! ARGV

    p options
  end

  describe 'The test website should be displayed' do

    it 'should go to google' do
      $ie = Watir::Browser.new($b)
      #go to test website
  $ie.goto("www.google.com")
    end
  end
end

执行 rspec ietest.rb --browser firefox -f doc只是给了我无效的选项,ietest 是我的文件名.欢迎任何其他通过网络驱动程序设置浏览器的直观方式,无需更改脚本代码.

Executing rspec ietest.rb --browser firefox -f doc just gives me invalid option, ietest is the name of my file. Any other intuitive ways of setting a browser through web driver, with out changing script code, would be welcome.

推荐答案

您不能将 rspec 与 OptionParser 一起使用,因为 rspec 可执行文件本身会解析自己的选项.您不能在 rspec 选项上捎带"您的选项.

You cannot use rspec with OptionParser since the rspec executable itself parses its own options. You cannot "piggy back" your options on the rspec options.

如果您必须执行类似操作,请使用设置文件(spec_config.yml 或类似文件),或使用环境变量:

If you must do something like this then use either a settings file (spec_config.yml or similar), or use an environment variable:

BROWSER=firefox spec test_something.rb

然后在您的代码中,您可以使用 ENV['BROWSER'] 来检索设置.

And then in your code you can use ENV['BROWSER'] to retrieve the setting.

这篇关于无法使用 OptionParser 和 rspec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆