(以下程式碼會呼叫 Ruby on Rails 的日誌功能。如果您未使用 Rails,請將這些呼叫替換為 puts
或 warn
。)
將以下內容新增為 features/support/debugging.rb
的內容,有助於除錯失敗的步驟
# rubocop:disable Lint/Debugger
class CucumberCounters
@error_counter = 0
@step_counter = 0
@screenshot_counter = 0
class << self
attr_accessor :error_counter, :step_counter, :screenshot_counter
end
end
# `LAUNCHY=1 cucumber` to open save screenshot after every step
After do |scenario|
next unless (ENV['LAUNCHY'] || ENV['CI']) && scenario.failed?
puts "Opening snapshot for #{scenario.name}"
begin
save_and_open_screenshot
rescue StandardError
puts "Can't save screenshot"
end
begin
save_and_open_page
rescue StandardError
puts "Can't save page"
end
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
Cucumber::Core::Test::Action.class_eval do
## first make sure we don't lose original accept method
unless instance_methods.include?(:orig_failed)
alias_method :orig_failed, :failed
end
## wrap original accept method to catch errors in executed step
def failed(*args)
begin
CucumberCounters.error_counter += 1
file_name = format('tmp/capybara/error_%03d.png',
CucumberCounters.error_counter)
Capybara.page.save_screenshot(file_name, full: true)
rescue
Rails.logger.info('[Cucumber] Can not make screenshot of failure')
end
binding.pry if ENV['DEBUG']
orig_failed(*args)
end
end
# Store the current scenario name as an instance variable, to make it
# available to the other hooks.
Before do |scenario|
case scenario
when Cucumber::Ast::Scenario
@scenario_name = scenario.name
when Cucumber::Ast::OutlineTable::ExampleRow
@scenario_name = scenario.scenario_outline.name
end
Rails.logger.info("[Cucumber] starting the #{@scenario_name}")
end
# `STEP=1 cucumber` to pause after each step
AfterStep do |scenario|
next unless ENV['STEP']
unless defined?(@counter)
puts "Stepping through #{@scenario_name}"
@counter = 0
end
@counter += 1
print "After step ##{@counter}/#{scenario.send(:steps).try(:count)}: "\
"#{scenario.send(:steps).to_a[@counter].try(:name) ||
'[RETURN to continue]'}..."
STDIN.getc
end
AfterStep do |scenario|
CucumberCounters.step_counter += 1
step = CucumberCounters.step_counter
file_name = format('tmp/capybara/step_%03d.png', step)
Rails.logger.info("[Cucumber] after step: #{@scenario_name}, step: #{step}")
next unless scenario.source_tag_names.include?('@intermittent')
begin
Capybara.page.save_screenshot(file_name, full: true)
Rails.logger.info("[Cucumber] Screenshot #{step} saved")
rescue
Rails.logger.info("[Cucumber] Can not make screenshot of #{step}")
end
end
AfterStep do
begin
execute_script "$(window).unbind('beforeunload')"
rescue => e
Rails.logger.error("An error was encountered and rescued")
Rails.logger.error(e.backtrace)
end
end
def dismiss_nav_warning
execute_script "$(window).unbind('beforeunload')"
wait_until_jquery_inactive
end
def wait_until_jquery_inactive
Capybara.using_wait_time(Capybara.default_max_wait_time) do
page.evaluate_script('jQuery.active').zero?
end
end
透過設定環境變數,您可以指示 Cucumber 使用各種除錯工具,並且可以透過設定多個環境變數來組合它們。
為了在 JVM 上除錯您的情境,您可以在除錯模式下逐步執行每個情境的步驟。
在您要除錯的程式碼部分設定中斷點。這可能是您目前遇到例外狀況的那一行(請參閱您的堆疊追蹤)。
在除錯模式下執行您的 RunCucumberTest
執行將在您的中斷點停止。
現在您可以選擇
- 逐步進入 開始除錯實作情境第一個步驟的方法。
- 或 繼續執行 執行目前步驟並跳到下一個步驟。
依此類推..
有關如何在您的 IDE 中設定中斷點的更多詳細資訊,請參閱
為了在 JVM 上除錯您的情境,您可以在除錯模式下逐步執行每個情境的步驟。
在您要除錯的程式碼部分設定中斷點。這可能是您目前遇到例外狀況的那一行(請參閱您的堆疊追蹤)。
在除錯模式下執行您的 RunCucumberTest
執行將在您的中斷點停止。
現在您可以選擇
- 逐步進入 開始除錯實作情境第一個步驟的方法。
- 或 繼續執行 執行目前步驟並跳到下一個步驟。
依此類推..
有關如何在您的 IDE 中設定中斷點的更多詳細資訊,請參閱
為了在 JVM 上除錯您的情境,您可以在除錯模式下逐步執行每個情境的步驟。
在您要除錯的程式碼部分設定中斷點。這可能是您目前遇到例外狀況的那一行(請參閱您的堆疊追蹤)。
在除錯模式下執行您的 RunCucumberTest
執行將在您的中斷點停止。
現在您可以選擇
- 逐步進入 開始除錯實作情境第一個步驟的方法。
- 或 繼續執行 執行目前步驟並跳到下一個步驟。
依此類推..
有關如何在您的 IDE 中設定中斷點的更多詳細資訊,請參閱