无法使用 OptionParser 和 rspec

发布于 2024-11-29 19:35:05 字数 815 浏览 1 评论 0原文

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

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 是我的文件名。任何其他通过网络驱动程序设置浏览器而无需更改脚本代码的直观方法都将受到欢迎。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-12-06 19:35:05

您不能将 rspec 与 OptionParser 一起使用,因为 rspec 可执行文件本身会解析自己的选项。您不能在 rspec 选项上“搭载”您的选项。

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

BROWSER=firefox spec test_something.rb

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

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.

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

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

幸福丶如此 2024-12-06 19:35:05

请了解 RSpec,因为我猜您对此一无所知(只需谷歌一下即可)。没有任何期望,您正在其中编写您的功能。

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

options = {}

opts = OptionParser.new do |opts|

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

opts.parse! ARGV

p options

ie = Watir::Browser.new(options[:browser].to_s)
#go to test website
ie.goto("www.google.com")

那应该有效。

编辑:如果您想测试它,请执行以下操作:

def open_url_with_browser(url, browser = 'firefox')
  nav = Watir::Browser.new(browser)
  nav.goto(url)
end

然后您将在规范中测试该方法。只需在不同的规范中存根 newgoto 即可。

如果您仍然想知道为什么会收到无效选项,是因为您将 --browser 传递给 rspec,而不是按预期传递给您的脚本。

Please, learn about RSpec because I am guessing you have no clue about it (just google it). There are no expectations and you are writing your functionality in it.

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

options = {}

opts = OptionParser.new do |opts|

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

opts.parse! ARGV

p options

ie = Watir::Browser.new(options[:browser].to_s)
#go to test website
ie.goto("www.google.com")

That should work.

EDIT: If you want to test it do something like this:

def open_url_with_browser(url, browser = 'firefox')
  nav = Watir::Browser.new(browser)
  nav.goto(url)
end

Then you would test that method in a spec. Just stub new, and goto in different specs.

If you are still wondering why are you getting the invalid option is because you are passing --browser to rspec, not your script, as intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文