Skip to content

Instantly share code, notes, and snippets.

@vojtad
Created March 5, 2025 13:20
Show Gist options
  • Save vojtad/c48728bf3813d8ffdf1899069c061fa8 to your computer and use it in GitHub Desktop.
Save vojtad/c48728bf3813d8ffdf1899069c061fa8 to your computer and use it in GitHub Desktop.
BrowserScreencast module for RSpec to capture screenscast using Ferrum screencast functionality in configure screenshots_dir.
# frozen_string_literal: true
module BrowserScreencast
# Save screencast images from Ferrum
def display_image(html:, screenshot_output:)
message = super
[
"[Screencast]: #{screencast_dir}",
message
].join("\n")
end
def ferrum_page
page.driver.browser.page
end
def screencast_dir
Rails.root.join(screenshots_dir, 'screencast').relative_path_from(Rails.root).to_s.tap do |dir|
FileUtils.mkpath(dir)
end
end
def start_screencast
ferrum_page.start_screencast(format: :jpeg, quality: 75) do |data, metadata|
timestamp = (metadata['timestamp'] * 1000).to_i
File.binwrite(File.join(screencast_dir, "image_#{timestamp}.jpg"), Base64.decode64(data))
end
end
delegate :stop_screencast, to: :ferrum_page
end
RSpec.configure do |config|
config.include BrowserScreencast, type: :system
config.before(:each, type: :system) do
start_screencast
end
config.after(:each, type: :system) do
stop_screencast
# remove screencast if test passed
FileUtils.rm_rf(screencast_dir) if passed?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment