From a11538d9469df558803187b90fdaa80208d43a0f Mon Sep 17 00:00:00 2001 From: rockwellll Date: Sun, 27 Jul 2025 11:15:20 +0300 Subject: [PATCH 1/6] add support for specifying the area to capture with an enum of viewport and full --- .gitignore | 1 + lib/kleya/browser.rb | 8 +++----- test.jpg | 1 - test/browser_test.rb | 23 +++++++++++++++++++++-- 4 files changed, 25 insertions(+), 8 deletions(-) delete mode 100644 test.jpg diff --git a/.gitignore b/.gitignore index ef99224..7a72f11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/* Gemfile.lock +test.jpg diff --git a/lib/kleya/browser.rb b/lib/kleya/browser.rb index 8890e20..a02b792 100644 --- a/lib/kleya/browser.rb +++ b/lib/kleya/browser.rb @@ -30,6 +30,7 @@ def initialize(**options) # @option options [Symbol] :format (:jpeg) image format (:jpeg, :png) # @option options [Integer] :quality (90) JPEG quality (1-100) # @option options [Symbol] :encoding (:base64) output encoding + # @option options [Symbol] :area (:viewport) the area to capture (:viewport, :full) # @return [Artifact] the screenshot artifact # @example Taking a X-optimized screenshot # browser = Kleya::Browser.new( @@ -43,12 +44,9 @@ def capture(url, options = {}) format = options[:format] || :jpeg quality = options[:quality] || 90 encoding = options[:encoding] || :base64 + full = options[:area] == :full - data = browser.screenshot( - format: format, - quality: quality, - encoding: encoding - ) + data = browser.screenshot(format:, quality:, encoding:, full:) Artifact.new(data:, url:, viewport: @viewport, format:, quality:, encoding:) rescue Ferrum::TimeoutError diff --git a/test.jpg b/test.jpg deleted file mode 100644 index 30d74d2..0000000 --- a/test.jpg +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/test/browser_test.rb b/test/browser_test.rb index 2a6d2b6..d9b5390 100644 --- a/test/browser_test.rb +++ b/test/browser_test.rb @@ -61,7 +61,7 @@ def test_capture_returns_artifact_with_defaults @mock_ferrum.expect :goto, nil, ['https://example.com'] # The screenshot method receives keyword arguments as a hash @mock_ferrum.expect :screenshot, 'fake_image_data' do |args| - args == { format: :jpeg, quality: 90, encoding: :base64 } + args == { format: :jpeg, quality: 90, encoding: :base64, full: false } end artifact = browser.capture('https://example.com') @@ -84,7 +84,7 @@ def test_capture_with_custom_options @mock_ferrum.expect :goto, nil, ['https://example.com'] # The screenshot method receives keyword arguments as a hash @mock_ferrum.expect :screenshot, 'fake_png_data' do |args| - args == { format: :png, quality: 100, encoding: :binary } + args == { format: :png, quality: 100, encoding: :binary, full: false } end artifact = browser.capture('https://example.com', @@ -100,6 +100,25 @@ def test_capture_with_custom_options @mock_ferrum.verify end + + def test_capture_with_full_area + browser = Kleya::Browser.new + + Ferrum::Browser.stub :new, @mock_ferrum do + @mock_ferrum.expect :goto, nil, ['https://example.com'] + # The screenshot method receives keyword arguments as a hash + @mock_ferrum.expect :screenshot, 'fake_full_image_data' do |args| + args == { format: :jpeg, quality: 90, encoding: :base64, full: true } + end + + artifact = browser.capture('https://example.com', area: :full) + + assert_instance_of Kleya::Artifact, artifact + assert_equal('fake_full_image_data', artifact.instance_variable_get(:@data)) + end + + @mock_ferrum.verify + end def test_capture_handles_timeout_error browser = Kleya::Browser.new From 29fc35ab2c68079104a1556299c766f7f8d2d3c6 Mon Sep 17 00:00:00 2001 From: rockwellll Date: Sun, 27 Jul 2025 11:16:13 +0300 Subject: [PATCH 2/6] update enum from full to page --- lib/kleya/browser.rb | 4 ++-- test/browser_test.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/kleya/browser.rb b/lib/kleya/browser.rb index a02b792..7737f75 100644 --- a/lib/kleya/browser.rb +++ b/lib/kleya/browser.rb @@ -30,7 +30,7 @@ def initialize(**options) # @option options [Symbol] :format (:jpeg) image format (:jpeg, :png) # @option options [Integer] :quality (90) JPEG quality (1-100) # @option options [Symbol] :encoding (:base64) output encoding - # @option options [Symbol] :area (:viewport) the area to capture (:viewport, :full) + # @option options [Symbol] :area (:viewport) the area to capture (:viewport, :page) # @return [Artifact] the screenshot artifact # @example Taking a X-optimized screenshot # browser = Kleya::Browser.new( @@ -44,7 +44,7 @@ def capture(url, options = {}) format = options[:format] || :jpeg quality = options[:quality] || 90 encoding = options[:encoding] || :base64 - full = options[:area] == :full + full = options[:area] == :page data = browser.screenshot(format:, quality:, encoding:, full:) diff --git a/test/browser_test.rb b/test/browser_test.rb index d9b5390..d6f401b 100644 --- a/test/browser_test.rb +++ b/test/browser_test.rb @@ -111,7 +111,7 @@ def test_capture_with_full_area args == { format: :jpeg, quality: 90, encoding: :base64, full: true } end - artifact = browser.capture('https://example.com', area: :full) + artifact = browser.capture('https://example.com', area: :page) assert_instance_of Kleya::Artifact, artifact assert_equal('fake_full_image_data', artifact.instance_variable_get(:@data)) From 7c0632bf5bc31e9ff95c4672686ae610aba45062 Mon Sep 17 00:00:00 2001 From: rockwellll Date: Sun, 27 Jul 2025 11:18:17 +0300 Subject: [PATCH 3/6] update readme --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f32c408..3438639 100644 --- a/README.md +++ b/README.md @@ -109,9 +109,10 @@ Kleya.capture('https://www.hellotext.com') Alongside the options you pass for the instance, there's some extra configurable settings you can tweak to your usecase. -- `format`: specifies the format of the image captures, i.e `jpeg` or `png`. -- `encoding`: specifies the encoding of the image, possible options is `binary` or `base64` (default). Regardless, the `Kleya::Artifact` object responds to `#binary` and `base64` when needed. -- `quality`: an integer between 1 - 100 that determines the quality of the final image, higher quality images result in bigger sizes and may not work correctly in some situations such as the Open Graph (OG) protocol, you can tweak and test this. Defaults to `90`. +- `format` specifies the format of the image captures, i.e `jpeg` or `png`. +- `encoding` specifies the encoding of the image, possible options is `binary` or `base64` (default). Regardless, the `Kleya::Artifact` object responds to `#binary` and `base64` when needed. +- `quality` an integer between 1 - 100 that determines the quality of the final image, higher quality images result in bigger sizes and may not work correctly in some situations such as the Open Graph (OG) protocol, you can tweak and test this. Defaults to `90`. +- `area` specifies the area of the browser to capture, defaults to `viewport` for capturing only the visible part of the page. Supported values are `viewport` and `page` for full-page screenshots. ```ruby artifact = browser.capture('https://example.com', format: :jpeg, quality: 85, encoding: :base64) From 1d6b3143d9766d68a530f026e58b07fc557eb0ac Mon Sep 17 00:00:00 2001 From: rockwellll Date: Sun, 27 Jul 2025 11:19:08 +0300 Subject: [PATCH 4/6] update readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 3438639..ba33a2b 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,6 @@ end ## Roadmap - Wait strategies (`wait_for: '.element'`, `wait_until: :network_idle`) -- Full page screenshots (not just viewport) - Built-in retry mechanism with configurable delays - Memory usage optimization for large batches From 8b376f45b71b9992d2cb1db2ac7f7e3b24501906 Mon Sep 17 00:00:00 2001 From: rockwellll Date: Sun, 27 Jul 2025 11:21:11 +0300 Subject: [PATCH 5/6] update readme --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index ba33a2b..113c2d2 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ browser.quit Kleya includes convenient viewport presets for social media platforms and common devices. You can pass any of the following values when initializing a browser instance. -- `desktop`: default (1920x1080) +- `desktop` default (1920x1080) - `x` (1200x675) - `facebook` (1200x630) @@ -186,7 +186,6 @@ end - Memory usage optimization for large batches - Request blocking (ads, analytics, fonts) -- Custom user agents for mobile rendering - CLI tool for quick captures (`kleya capture https://example.com`) - Debug mode with browser preview From da76ca4bf9fe84802c6ea254374b36fae1b1bc98 Mon Sep 17 00:00:00 2001 From: rockwellll Date: Sun, 27 Jul 2025 11:22:42 +0300 Subject: [PATCH 6/6] update version --- kleya.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kleya.gemspec b/kleya.gemspec index 946df30..7087fbb 100644 --- a/kleya.gemspec +++ b/kleya.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'kleya' - s.version = '0.0.1' + s.version = '0.0.2' s.summary = 'Screenshots, made easy.' s.description = 'Screenshots, made easy.' s.authors = ['Hellotext', 'Ahmed Khattab']