无法使用 OptionParser 和 rspec
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将 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.请了解 RSpec,因为我猜您对此一无所知(只需谷歌一下即可)。没有任何期望,您正在其中编写您的功能。
那应该有效。
编辑:如果您想测试它,请执行以下操作:
然后您将在规范中测试该方法。只需在不同的规范中存根
new
和goto
即可。如果您仍然想知道为什么会收到无效选项,是因为您将
--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.
That should work.
EDIT: If you want to test it do something like this:
Then you would test that method in a spec. Just stub
new
, andgoto
in different specs.If you are still wondering why are you getting the invalid option is because you are passing
--browser
torspec
, not your script, as intended.