我正在使用Ruby Rspec来测试一个没有使用rails的webapp,因此我没有rails_helper.rb,只是一个spec_helper.rb。我可以手动触发屏幕截图,但我无法弄清楚如何在失败时触发屏幕截图。
spec_helper.rb:
# frozen-string-literal: true
require 'rspec'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'site_prism'
Dir[File.dirname(__FILE__) + '/page_objects/*/*.rb'].each do |page_object|
require page_object
end
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.save_path = "#{Dir.pwd}/screenshots"
Capybara.default_driver = :selenium
Capybara.app_host = "REDACTED"
Capybara.default_max_wait_time = 20
my_spec.rb
describe 'spec' do
before(:each) do
@my_page = login_and_open_my_page
end
it "fails" do
expect(5).to eq(6)
end
end
答案 0 :(得分:1)
您应该能够在阻止之后在RSpec中执行此操作。您可以在RSpec配置等中定义它,并且应该基本归结为
after(:each) do |example|
if example.exception
# Do whatever you want to happen on failure
Capybara.current_session.save_and_open_screenshot
end
end
在重置会话之前,您需要运行该块,因此需要在为执行重置而定义的任何块之后定义该块。