RubyMine - 如何调试 Cucumber 步骤文件?

3
RubyMine - 如何调试Cucumber步骤文件?我已经谷歌过这个问题,但是我找到的任何信息都没有对我有用。在使用RubyMine时,有人成功调试了步骤文件吗?
谢谢。

开箱即用。您遇到了什么具体问题?请注意,您无法调试功能文件。 - CrazyCoder
5个回答

1
在我的情况下,Gemfile 文件中没有将 'debugger' gem 放在正确的组中。
请确保:
group :development, :test do
  gem 'debugger'
end

不再建议使用调试器宝石,可以尝试使用debugger2


1

在你的Gemfile中添加'pry'

在你的启动/钩子文件中,靠近顶部或者其他地方加入'require 'pry''

在你想要调试的步骤中,在你遇到问题的地方加入:binding.pry

这会将你转移到一个REPL环境中,你可以检查所有本地变量等。如果你正在使用selenium与浏览器交互,你还可以查看浏览器,使用'检查元素',然后看看能否使用selenium调用找到该元素。

你还可以制作一个步骤(我的叫做'And I debug'),它什么也不做,只是调用binding.pry;这对于暂停selenium自动化很有用。要退出REPL,请使用'exit'或^D。

你需要从命令行运行你的cukes才能有效地使用它;不确定rubymine是否具备这个功能。


1

这是一个非常普遍的问题,在网上有很多解决方法,但是都没有成功。您不需要新的宝石,也不需要更改设置。

很可能,您所有的功能文件都在根文件夹中。

听起来很荒谬,但创建一个新文件夹,在其中放置功能文件(称其为通用名称,如“tests”)。

设置断点,然后再试一次。这应该可以解决问题。


1
将以下内容作为features/support/debugging.rb的内容,可以帮助调试失败的步骤:
# `LAUNCHY=1 cucumber` to open page on failure
After do |scenario|
  save_and_open_page if scenario.failed? && ENV['LAUNCHY']
end

# `FAST=1 cucumber` to stop on first failure
After do |scenario|
  Cucumber.wants_to_quit = ENV['FAST'] && scenario.failed?
end

# `DEBUG=1 cucumber` to drop into debugger on failure
After do |scenario|
  next unless ENV['DEBUG'] && scenario.failed?
  puts "Debugging scenario: #{scenario.title}"
  if respond_to? :debugger
    debugger
  elsif binding.respond_to? :pry
    binding.pry
  else
    puts "Can't find debugger or pry to debug"
  end 
end

# `STEP=1 cucumber` to pause after each step
AfterStep do |scenario|
  next unless ENV['STEP']
  unless defined?(@counter)
    puts "Stepping through #{scenario.title}"
    @counter = 0
  end
  @counter += 1
  print "At step ##{@counter} of #{scenario.steps.count}. Press Return to"\
        ' execute...'
  STDIN.getc
end

通过设置环境变量,您可以使Cucumber使用各种调试工具,并且可以通过设置多个环境变量来组合它们。

-1

不是RubyMine,但如果您熟悉CLI,则debugger gem会有很大帮助。

[sudo] gem install debugger

然后,在问题所在的LOC之前放置关键字debugger,像平常一样运行cucumber。 debugger将在您遇到问题的LOC之前停止。

我经常使用这种方法来解决Cucumber / WATIR的问题。

https://github.com/cldwalker/debugger


什么是LOC?我在我的步骤定义中放置了“debugger”,但执行没有停止。 - Green

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接