Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/*
Gemfile.lock
test.jpg
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kleya.gemspec
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
8 changes: 3 additions & 5 deletions lib/kleya/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion test.jpg

This file was deleted.

23 changes: 21 additions & 2 deletions test/browser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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',
Expand All @@ -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
Expand Down