本文介绍了运行黄瓜时带干运行选项时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图通过打开和关闭浏览器一次来提高运行测试的速度。 http://watirmelon.com/2012/04/01/five-page-object-anti-patterns/ 我在hooks.rb中有以下内容 = Watir :: Browser.new ENV ['BROWSER']。to_sym before do @browser = browser end | @ browser.cookies.clear end at_exit do browser.close end 它大大提高了测试套件运行的速度。但是我还需要从我的自定义格式化程序在json文件中的一些统计信息,而不运行测试: cucumber -t @test -d -f Cucumber :: Formatter :: MyCustomFormatter>问题是黄瓜catches错误与 -d code>选项: 未初始化的常量Watir(NameError) / home / user / work / 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:在`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:在`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(必需)>' /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:在`< main>' 移动之前的方法之前阻止: 之前的操作 if!$ somevar browser = Watir :: Browser.new ENV ['BROWSER']。to_sym @browser = browser $ somevar = true end end code> at_exit 块: / home / user / work /支持/ hooks.rb:26:in'block in< top(required)>':undefined local variable或method`browser'for main:Object(NameError) pre> 如果我把 @ 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 选项一起使用? $ b $问题是干运行(-d)不加载你的support / env.rb文件。如果你 cucumber -h 看到帮助,它说: d,--dry-run调用formatters而不执行步骤。这也省略了加载你的support / env.rb文件,如果它存在的话。 因为你需要在env中的watir-webdriver。 rb和env.rb没有加载,那么你的hooks文件将不知道什么是Watir。 一个解决方案是添加 require' 另一个解决方案是将浏览器创建和at_exit钩子移动到env.rb文件中。这样,当使用干运行选项时,您将看不到浏览器打开和关闭。 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.rbbrowser = Watir::Browser.new ENV['BROWSER'].to_symBefore do @browser = browserendAfter do |scenario| @browser.cookies.clearendat_exit do browser.closeendIt 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.jsonThe 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 endendAnd 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? 解决方案 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. 这篇关于运行黄瓜时带干运行选项时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 17:56