Skip to content

Commit 7c84cdb

Browse files
authored
Merge pull request #148 from rubycdp/feature/screenshot_with_specific_background_color
Screenshot with specific background color
2 parents 2e46616 + bc99496 commit 7c84cdb

File tree

5 files changed

+130
-3
lines changed

5 files changed

+130
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ Saves screenshot on a disk or returns it as base64.
335335
* :full `Boolean` whether you need full page screenshot or a viewport
336336
* :selector `String` css selector for given element
337337
* :scale `Float` zoom in/out
338+
* :background_color `Ferrum::RGBA.new(0, 0, 0, 0.0)` to have specific background color
338339

339340
```ruby
340341
browser.go_to("https://google.com/")
@@ -344,6 +345,8 @@ browser.screenshot(path: "google.png") # => 134660
344345
browser.screenshot(path: "google.jpg") # => 30902
345346
# Save to Base64 the whole page not only viewport and reduce quality
346347
browser.screenshot(full: true, quality: 60) # "iVBORw0KGgoAAAANSUhEUgAABAAAAAMACAYAAAC6uhUNAAAAAXNSR0IArs4c6Q...
348+
# Save with specific background color
349+
browser.screenshot(background_color: Ferrum::RGBA.new(0, 0, 0, 0.0))
347350
```
348351

349352
#### pdf(\*\*options) : `String` | `Boolean`

lib/ferrum/page/screenshot.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "ferrum/rbga"
4+
35
module Ferrum
46
class Page
57
module Screenshot
@@ -29,7 +31,7 @@ module Screenshot
2931
def screenshot(**opts)
3032
path, encoding = common_options(**opts)
3133
options = screenshot_options(path, **opts)
32-
data = capture_screenshot(options, opts[:full])
34+
data = capture_screenshot(options, opts[:full], opts[:background_color])
3335
return data if encoding == :base64
3436

3537
bin = Base64.decode64(data)
@@ -170,9 +172,11 @@ def to_camel_case(option)
170172
option.to_s.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.to_sym
171173
end
172174

173-
def capture_screenshot(options, full)
175+
def capture_screenshot(options, full, background_color)
174176
maybe_resize_fullscreen(full) do
175-
command("Page.captureScreenshot", **options)
177+
with_background_color(background_color) do
178+
command("Page.captureScreenshot", **options)
179+
end
176180
end.fetch("data")
177181
end
178182

@@ -186,6 +190,18 @@ def maybe_resize_fullscreen(full)
186190
ensure
187191
resize(width: width, height: height) if full
188192
end
193+
194+
def with_background_color(color)
195+
if color
196+
raise ArgumentError, "Accept Ferrum::RGBA class only" unless color.is_a?(RGBA)
197+
198+
command('Emulation.setDefaultBackgroundColorOverride', color: color.to_h)
199+
end
200+
201+
yield
202+
ensure
203+
command('Emulation.setDefaultBackgroundColorOverride') if color
204+
end
189205
end
190206
end
191207
end

lib/ferrum/rbga.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Ferrum
2+
class RGBA
3+
def initialize(red, green, blue, alpha)
4+
self.red = red
5+
self.green = green
6+
self.blue = blue
7+
self.alpha = alpha
8+
9+
validate
10+
end
11+
12+
def to_h
13+
{ r: red, g: green, b: blue, a: alpha }
14+
end
15+
16+
private
17+
18+
attr_accessor :red, :green, :blue, :alpha
19+
20+
def validate
21+
[red, green, blue].each(&method(:validate_color))
22+
validate_alpha
23+
end
24+
25+
def validate_color(value)
26+
return if value && value.is_a?(Integer) && Range.new(0, 255).include?(value)
27+
28+
raise ArgumentError, "Wrong value of #{value} should be Integer from 0 to 255"
29+
end
30+
31+
def validate_alpha
32+
return if alpha && alpha.is_a?(Float) && Range.new(0.0, 1.0).include?(alpha)
33+
34+
raise ArgumentError,
35+
"Wrong alpha value #{alpha} should be Float between 0.0 (fully transparent) and 1.0 (fully opaque)"
36+
end
37+
end
38+
end

spec/rbga_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require "ferrum/rbga"
4+
5+
module Ferrum
6+
describe RGBA do
7+
describe '#to_h' do
8+
it { expect(RGBA.new(0, 0, 0, 0.0).to_h).to eq({ r: 0, g: 0, b: 0, a: 0.0 }) }
9+
it { expect(RGBA.new(255, 255, 255, 1.0).to_h).to eq({ r: 255, g: 255, b: 255, a: 1.0 }) }
10+
end
11+
12+
it 'raises ArgumentError for not Float value of alpha' do
13+
expect {
14+
RGBA.new(0, 0, 0, 0)
15+
}.to raise_exception(
16+
ArgumentError,
17+
'Wrong alpha value 0 should be Float between 0.0 (fully transparent) and 1.0 (fully opaque)')
18+
end
19+
20+
it 'raises ArgumentError wrong value of alpha' do
21+
expect {
22+
RGBA.new(0, 0, 0, 2.0)
23+
}.to raise_exception(
24+
ArgumentError,
25+
'Wrong alpha value 2.0 should be Float between 0.0 (fully transparent) and 1.0 (fully opaque)')
26+
end
27+
28+
it 'raises ArgumentError for wrong value of red' do
29+
expect {
30+
RGBA.new(-1, 0, 0, 0.0)
31+
}.to raise_exception(ArgumentError, 'Wrong value of -1 should be Integer from 0 to 255')
32+
end
33+
34+
it 'raises ArgumentError for wrong value of green' do
35+
expect {
36+
RGBA.new(0, 256, 0, 0.0)
37+
}.to raise_exception(ArgumentError, 'Wrong value of 256 should be Integer from 0 to 255')
38+
end
39+
40+
it 'raises ArgumentError for wrong value of blue' do
41+
expect {
42+
RGBA.new(0, 0, 0.0, 0.0)
43+
}.to raise_exception(ArgumentError, 'Wrong value of 0.0 should be Integer from 0 to 255')
44+
end
45+
end
46+
end

spec/screenshot_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "image_size"
44
require "pdf/reader"
55
require "chunky_png"
6+
require "ferrum/rbga"
67

78
module Ferrum
89
describe Browser do
@@ -176,6 +177,29 @@ def create_screenshot(**options)
176177
end
177178
end
178179

180+
describe 'background_color option' do
181+
it 'supports screenshotting page with the specific background color' do
182+
begin
183+
file = PROJECT_ROOT + "/spec/tmp/screenshot.jpeg"
184+
browser.go_to
185+
browser.screenshot(path: file)
186+
content = File.read(file)
187+
browser.screenshot(path: file, background_color: RGBA.new(0, 0, 0, 0.0))
188+
content_with_specific_bc = File.read(file)
189+
expect(content).not_to eq(content_with_specific_bc)
190+
ensure
191+
FileUtils.rm_f([file])
192+
end
193+
end
194+
195+
it 'raises ArgumentError with proper message' do
196+
browser.go_to
197+
expect {
198+
browser.screenshot(path: file, background_color: '#FFF')
199+
}.to raise_exception(ArgumentError, 'Accept Ferrum::RGBA class only')
200+
end
201+
end
202+
179203
shared_examples "when scale is set" do
180204
it "changes image dimensions" do
181205
browser.go_to("/ferrum/zoom_test")

0 commit comments

Comments
 (0)