My goal is to have an rspec system spec run with rack_test and to save the html on failure to be able to investigate.
This is possible for selenium driver, but I can’t find a way to make it work for rack_test and I feel like I am missing something so I am suggesting this new feature.
I would imagine that it should be possible to go to spec/rails_helper.rb and define
RSpec.configure do |config|
config.after(:each) do
save_html? # this does not work
end
end
In this way we can save the html of the page. The problem is the page.body at this stage is “”, because the method Capybara.reset_session! was called. And it is called here:
# frozen_string_literal: true
module ActionDispatch
module SystemTesting
module TestHelpers
module SetupAndTeardown # :nodoc:
def host!(host)
ActiveSupport::Deprecation.warn \
"ActionDispatch::SystemTestCase#host! is deprecated with no replacement. " \
"Set Capybara.app_host directly or rely on Capybara's default host."
Capybara.app_host = host
end
def before_teardown
take_failed_screenshot
ensure
super
end
def after_teardown
Capybara.reset_sessions!
ensure
super
end
end
end
end
end
This means that the session is reset and only then the config(:after) in the spec/rails_helper is executed.
The call to take_failed_screenshot is
44: def take_failed_screenshot
45: take_screenshot if failed? && supports_screenshot?
46: end
which checks if screenshot is supported
[128, 137] in /home/kireto/.rvm/gems/ruby-2.7.4/gems/actionpack-6.1.5/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb
128: def failed?
129: !passed? && !skipped?
130: end
131:
132: def supports_screenshot?
=> 133: Capybara.current_driver != :rack_test
134: end
135: end
136: end
137: end
Because the driver is rack_test we naturally skip the rest. And after that the session is reset with Capybara.reset_session! and after that our config.after(:each) block is called and there is no longer a session and a page and the html can not be saved.
I understand that a screenshot can not be taken with the rack_test. That’s find. But the html can be saved?
What do you think about introducing a feature where at least the HTML of the page could be saved for an rspec ran with rack_test?