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/README.md b/README.md index f32c408..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) @@ -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) @@ -181,12 +182,10 @@ 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 - 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 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'] diff --git a/lib/kleya/browser.rb b/lib/kleya/browser.rb index 8890e20..7737f75 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, :page) # @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] == :page - 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..d6f401b 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: :page) + + 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