Skip to content

Commit

Permalink
allow to save_to_clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
muriloime committed Feb 20, 2025
1 parent 4c9bd6c commit ed682e1
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 20 deletions.
46 changes: 45 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
PATH
remote: .
specs:
lookbook_visual_tester (0.1.1)
lookbook_visual_tester (0.1.2)
capybara
concurrent-ruby
cuprite
lookbook
mini_magick
rails
ruby-prof

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -88,6 +90,24 @@ GEM
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.2)
async (2.23.0)
console (~> 1.29)
fiber-annotation
io-event (~> 1.9)
metrics (~> 0.12)
traces (~> 0.15)
async-http (0.87.0)
async (>= 2.10.2)
async-pool (~> 0.9)
io-endpoint (~> 0.14)
io-stream (~> 0.6)
metrics (~> 0.12)
protocol-http (~> 0.49)
protocol-http1 (~> 0.30)
protocol-http2 (~> 0.22)
traces (~> 0.10)
async-pool (0.10.3)
async (>= 1.25)
base64 (0.2.0)
bigdecimal (3.1.8)
builder (3.3.0)
Expand All @@ -102,6 +122,10 @@ GEM
xpath (~> 3.2)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
console (1.29.3)
fiber-annotation
fiber-local (~> 1.1)
json
crass (1.0.6)
css_parser (1.21.0)
addressable
Expand All @@ -118,13 +142,20 @@ GEM
webrick (~> 1.7)
websocket-driver (~> 0.7)
ffi (1.17.0-x86_64-linux-gnu)
fiber-annotation (0.2.0)
fiber-local (1.1.0)
fiber-storage
fiber-storage (1.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
htmlbeautifier (1.4.3)
htmlentities (4.3.4)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
io-endpoint (0.15.2)
io-event (1.9.0)
io-stream (0.6.1)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
Expand Down Expand Up @@ -157,6 +188,7 @@ GEM
marcel (1.0.4)
matrix (0.4.2)
method_source (1.1.0)
metrics (0.12.1)
mini_magick (4.13.2)
mini_mime (1.1.5)
minitest (5.25.1)
Expand All @@ -177,6 +209,13 @@ GEM
parser (3.3.5.0)
ast (~> 2.4.1)
racc
protocol-hpack (1.5.1)
protocol-http (0.49.0)
protocol-http1 (0.30.0)
protocol-http (~> 0.22)
protocol-http2 (0.22.1)
protocol-hpack (~> 1.4)
protocol-http (~> 0.47)
psych (5.1.2)
stringio
public_suffix (6.0.1)
Expand Down Expand Up @@ -255,10 +294,12 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
parser (>= 3.3.1.0)
ruby-prof (1.7.1)
ruby-progressbar (1.13.0)
stringio (3.1.1)
thor (1.3.2)
timeout (0.4.3)
traces (0.15.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.6.0)
Expand All @@ -280,8 +321,11 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
async
async-http
bundler (~> 2.0)
capybara (~> 3.35)
concurrent-ruby
cuprite (~> 0.14)
lookbook
lookbook_visual_tester!
Expand Down
5 changes: 3 additions & 2 deletions lib/lookbook_visual_tester/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module LookbookVisualTester
class Configuration
attr_accessor :base_path, :baseline_dir, :current_dir, :diff_dir, :threads
attr_accessor :base_path, :baseline_dir, :current_dir, :diff_dir, :threads ,:host

DEFAULT_THREADS = 4

Expand All @@ -9,9 +9,10 @@ def initialize
@baseline_dir = @base_path.join("baseline")
@current_dir = @base_path.join("current_run")
@diff_dir = @base_path.join("diff")

@threads = DEFAULT_THREADS

@host = ENV["LOOKBOOK_HOST"] || "https://localhost:5000"

[baseline_dir, current_dir, diff_dir].each do |dir|
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
end
Expand Down
7 changes: 7 additions & 0 deletions lib/lookbook_visual_tester/scenario_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,12 @@ def current_path
def baseline_path
LookbookVisualTester.config.baseline_dir.join(filename)
end

def preview_url
Lookbook::Engine.routes.url_helpers.lookbook_preview_url(
path: preview.lookup_path + "/" + scenario.name,
host: LookbookVisualTester.config.host
)
end
end
end
26 changes: 16 additions & 10 deletions lib/lookbook_visual_tester/screenshot_taker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module LookbookVisualTester
class ScreenshotTaker
attr_reader :session, :logger

CLIPBOARD = 'clipboard'

def initialize(logger: Kernel)
Capybara.register_driver :cuprite do |app|
Capybara::Cuprite::Driver.new(
Expand All @@ -23,31 +25,35 @@ def initialize(logger: Kernel)
@logger = logger
end

def capture(preview_url, path)
def capture(preview_url, path = CLIPBOARD)
FileUtils.mkdir_p(File.dirname(path))

session.visit(preview_url)

# Wait for network requests to complete
# session.driver.network_idle?

# Wait for any loading indicators to disappear
begin
session.has_no_css?(".loading", wait: 10)
rescue StandardError
nil
end

# Additional wait for any JavaScript animations
sleep 1

save_screenshot(path)
if path == CLIPBOARD
save_to_clipboard
else
save_screenshot(path)
end
rescue StandardError => e
logger.puts "Error capturing screenshot for #{preview_url}: #{e.message}"
raise e
end

private
def save_to_clipboard
Tempfile.create(['screenshot', '.png']) do |file|
session.save_screenshot(file.path)

# Example: Copy to clipboard (Linux xclip)
system("xclip -selection clipboard -t image/png -i #{file.path}")
end
end

def save_screenshot(path)
session.save_screenshot(path)
Expand Down
2 changes: 1 addition & 1 deletion lib/lookbook_visual_tester/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module LookbookVisualTester
VERSION = "0.1.2"
VERSION = "0.1.3"
end
29 changes: 23 additions & 6 deletions lib/tasks/lookbook_visual_tester.rake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ namespace :lookbook_visual_tester do
printer.print(STDOUT)
end

desc "Run and copy to clipboard first scenario matching the given name"
task :copy, [:name] => :environment do |t, args|
# example on how to run: `rake lookbook_visual_tester:copy["Button"]`

screenshot_taker = LookbookVisualTester::ScreenshotTaker.new
previews = Lookbook.previews

regex = Regexp.new(args[:name].chars.join(".*"), Regexp::IGNORECASE)
matched_previews = previews.select { |preview| regex.match?(preview.name.underscore) }
if matched_previews.empty?
puts "No Lookbook previews found matching #{args[:name]}"
exit
end
matched_previews.each do |preview|
preview.scenarios.each do |scenario|
scenario_run = LookbookVisualTester::ScenarioRun.new(scenario)
screenshot_taker.capture(scenario_run.preview_url)
exit
end
end
end

desc "Run visual regression tests for Lookbook previews"
task run: :environment do
screenshot_taker = LookbookVisualTester::ScreenshotTaker.new
Expand All @@ -42,12 +64,7 @@ namespace :lookbook_visual_tester do
Concurrent::Promises.future_on(pool) do
scenario_run = LookbookVisualTester::ScenarioRun.new(scenario)

preview_url = Lookbook::Engine.routes.url_helpers.lookbook_preview_url(
path: preview.lookup_path + "/" + scenario.name,
host: ENV["LOOKBOOK_HOST"] || "https://localhost:5000"
)

screenshot_taker.capture(preview_url, scenario_run.current_path)
screenshot_taker.capture(scenario_run.preview_url, scenario_run.current_path)
puts " Visiting URL: #{preview_url}"

image_comparator.compare(scenario_run)
Expand Down

0 comments on commit ed682e1

Please sign in to comment.