gpt4 book ai didi

ruby - 使用空运行选项运行 cucumber 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:01 25 4
gpt4 key购买 nike

我试图通过只打开和关闭浏览器一次来提高运行测试的速度。

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

大大提高了测试套件的运行速度。但我还需要来 self 的自定义格式化程序的 json 文件中的一些统计信息,而不需要运行测试:

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

问题是 cucumber 使用 -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 block 中:

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

并在 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)

如果我将 @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)

有没有办法在测试套件中打开/关闭浏览器一次,并使其与 cucumber 的 -d 选项一起工作?

最佳答案

问题是试运行 (-d) 不会加载您的 support/env.rb 文件。如果你执行 cucumber -h 来查看帮助,它说:

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

由于您在 env.rb 中需要 watir-webdriver 并且 env.rb 未加载,因此您的 Hook 文件将不知道 Watir 是什么。

一种解决方案是将 require 'watir-webdriver'(或 require 'watir')添加到您的 hooks.rb 文件中。

另一种解决方案是将浏览器创建和 at_exit Hook 移动到 env.rb 文件中。这样,当使用试运行选项时,您将不会看到浏览器打开和关闭。

关于ruby - 使用空运行选项运行 cucumber 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com