我试图通过只打开和关闭浏览器一次来提高运行测试的速度。
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
选项一起使用?
答案 0 :(得分:2)
问题是dry-run(-d)不会加载你的support / env.rb文件。如果您cucumber -h
查看帮助,则会说:
-d, - dr-run在不执行步骤的情况下调用格式化程序。这也省略了support / env.rb文件的加载(如果存在)。
由于你在env.rb中需要watir-webdriver并且env.rb没有加载,所以你的钩子文件将不知道什么是Watir。
一种解决方案是将require 'watir-webdriver'
(或require 'watir'
)添加到您的hooks.rb文件中。
另一种解决方案是将浏览器创建和at_exit挂钩移动到env.rb文件中。这样,当使用干运行选项时,您将看不到浏览器的打开和关闭。