Skip to content

Instantly share code, notes, and snippets.

@timdiggins
Created February 23, 2019 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timdiggins/cdae6ff2b59af5a78383eb9ac1bfe3b5 to your computer and use it in GitHub Desktop.
Save timdiggins/cdae6ff2b59af5a78383eb9ac1bfe3b5 to your computer and use it in GitHub Desktop.
An equivalent to capybara save_and_open_page for request specs
# frozen_string_literal: true
RSpec.shared_context "save_and_open_page for request" do
def asset_host
"http://localhost:3000"
end
def save_and_open_page
Dir.mkdir(Rails.root.join("tmp/requests")) unless Dir.exist?(Rails.root.join("tmp/requests"))
file = Rails.root.join("tmp/requests/screenshot-#{Time.now.to_s(:number)}.html")
File.open(file, "wb") do |f|
f.write(response.body.gsub(/\<head\>/, "<head><base href='#{asset_host}'/>"))
end
Launchy.open("file:#{file}")
end
end
RSpec.configure do |config|
config.include_context "save_and_open_page for request", type: :request
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment