我试图通过只打开和关闭浏览器一次来提高运行测试的速度。
http://watirmelon.com/2012/04/01/five-page-object-anti-patterns/
我在 hooks.rb 中有以下内容
browser = Watir::Browser.new ENV['BROWSER'].to_sym
Before do
@browser = browser
end
After do |scenario|
@browser.cookies.clear
end
at_exit do
browser.close
end
它极大地提高了测试套件的运行速度。但是我还需要来自我的自定义格式化程序的 json 文件中的一些统计信息,而无需运行测试:
cucumber -t @test -d -f Cucumber::Formatter::MyCustomFormatter > result.json
问题是黄瓜使用-d
选项捕获错误:
uninitialized constant Watir (NameError)
/home/user/work/repository/features/support/hooks.rb:7:in `<top (required)>'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/lib/cucumber/rb_support/rb_language.rb:122:in `load'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/lib/cucumber/rb_support/rb_language.rb:122:in `load_code_file'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:180:in `load_file'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:83:in `block in load_files!'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:82:in `each'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:82:in `load_files!'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/lib/cucumber/runtime.rb:183:in `load_step_definitions'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/lib/cucumber/runtime.rb:42:in `run!'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/lib/cucumber/cli/main.rb:47:in `execute!'
/home/user/.rvm/gems/ruby-2.0.0-p247/gems/cucumber-1.3.5/bin/cucumber:13:in `<top (required)>'
/home/user/.rvm/gems/ruby-2.0.0-p247/bin/cucumber:23:in `load'
/home/user/.rvm/gems/ruby-2.0.0-p247/bin/cucumber:23:in `<main>'
/home/user/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/home/user/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
然后我尝试在块Watir.new
内移动方法Before
:
Before do
if !$somevar
browser = Watir::Browser.new ENV['BROWSER'].to_sym
@browser = browser
$somevar = true
end
end
at_exit
并在块中出错:
/home/user/work/repository/features/support/hooks.rb:26:in `block in <top (required)>': undefined local variable or method `browser' for main:Object (NameError)
如果我把@browser.close
里面at_exit
,我得到:
/home/user/work/repository/features/support/hooks.rb:26:in `block in <top (required)>': undefined method `close' for nil:NilClass (NoMethodError)
有没有办法在测试套件中打开/关闭浏览器并使其与-d
黄瓜选项一起使用?