Error when run cucumber with dry run option

2019-07-19 08:20发布

I'm trying to improve the speed of running tests by opening and closing the browser only once.

http://watirmelon.com/2012/04/01/five-page-object-anti-patterns/

I have the following in 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

It greatly boosts up the speed of test suite running. But I also need some statistics in json file from my custom formatter without running tests:

cucumber -t @test -d -f Cucumber::Formatter::MyCustomFormatter > result.json

The problem is that cucumber catches error with -d option:

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>'

Then I try to move Watir.new method inside Before block:

Before do
  if !$somevar
    browser = Watir::Browser.new ENV['BROWSER'].to_sym
    @browser = browser
    $somevar = true
  end
end

And get error in at_exit block:

/home/user/work/repository/features/support/hooks.rb:26:in `block in <top (required)>': undefined local variable or method `browser' for main:Object (NameError)

If I put @browser.close inside at_exit, I get:

/home/user/work/repository/features/support/hooks.rb:26:in `block in <top (required)>': undefined method `close' for nil:NilClass (NoMethodError)

Is there any way to open/close the browser once in a test suite and make it work with the -d option of cucumber?

标签: ruby cucumber
1条回答
我命由我不由天
2楼-- · 2019-07-19 08:47

The problem is that dry-run (-d) does not load your support/env.rb file. If you do cucumber -h to see the help, it says:

-d, --dry-run Invokes formatters without executing the steps. This also omits the loading of your support/env.rb file if it exists.

Since you require watir-webdriver in the env.rb and env.rb does not get loaded, then your hooks file will not know what Watir is.

One solution would be to add require 'watir-webdriver' (or require 'watir') to your hooks.rb file.

An alternative solution is to move the browser creation and at_exit hooks into the env.rb file. This way, when using the dry run option, you will not see the browser open and close.

查看更多
登录 后发表回答