Skip to content

Commit c305386

Browse files
committed
rename background_rgba_color to background_color with RGBA class
1 parent 3d88a81 commit c305386

File tree

3 files changed

+20
-45
lines changed

3 files changed

+20
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ browser.screenshot(path: "google.jpg") # => 30902
346346
# Save to Base64 the whole page not only viewport and reduce quality
347347
browser.screenshot(full: true, quality: 60) # "iVBORw0KGgoAAAANSUhEUgAABAAAAAMACAYAAAC6uhUNAAAAAXNSR0IArs4c6Q...
348348
# Save with specific background color
349-
browser.screenshot(background_rgba_color: [0, 0, 0, 0.0])
349+
browser.screenshot(background_color: Ferrum::RGBA.new(0, 0, 0, 0.0))
350350
```
351351

352352
#### pdf(\*\*options) : `String` | `Integer`

lib/ferrum/page/screenshot.rb

Lines changed: 9 additions & 18 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
@@ -27,7 +29,7 @@ module Screenshot
2729
def screenshot(**opts)
2830
path, encoding = common_options(**opts)
2931
options = screenshot_options(path, **opts)
30-
data = capture_screenshot(options, opts[:full], opts[:background_rgba_color])
32+
data = capture_screenshot(options, opts[:full], opts[:background_color])
3133
return data if encoding == :base64
3234

3335
bin = Base64.decode64(data)
@@ -123,21 +125,9 @@ def screenshot_options(path = nil, format: nil, scale: 1.0, **opts)
123125
options[:clip].merge!(scale: scale)
124126
end
125127

126-
if validate_background_rgba_color(opts[:background_rgba_color])
127-
raise ArgumentError, "Specify :background_rgba_color as [R,G,B,A] array"
128-
end
129-
130128
options
131129
end
132130

133-
def validate_background_rgba_color(option)
134-
option && !(
135-
option.is_a?(Array) &&
136-
option.size == 4 &&
137-
option.all? { |value| value.is_a?(Numeric) }
138-
)
139-
end
140-
141131
def get_bounding_rect(selector)
142132
rect = evaluate_async(%Q(
143133
const rect = document
@@ -174,15 +164,16 @@ def maybe_resize_fullscreen(full)
174164
resize(width: width, height: height) if full
175165
end
176166

177-
def with_background_color(background_rgba_color)
178-
if background_rgba_color
179-
r, g, b, a = background_rgba_color
180-
command('Emulation.setDefaultBackgroundColorOverride', color: { r: r, g: g, b: b, a: a })
167+
def with_background_color(color)
168+
if color
169+
raise ArgumentError, "Accept Ferrum::RGBA class only" unless color.is_a?(RGBA)
170+
171+
command('Emulation.setDefaultBackgroundColorOverride', color: color.to_h)
181172
end
182173

183174
yield
184175
ensure
185-
command('Emulation.setDefaultBackgroundColorOverride') if background_rgba_color
176+
command('Emulation.setDefaultBackgroundColorOverride') if color
186177
end
187178
end
188179
end

spec/screenshot_spec.rb

Lines changed: 10 additions & 26 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,44 +177,27 @@ def create_screenshot(**options)
176177
end
177178
end
178179

179-
context 'background_rgba_color option' do
180-
shared_examples 'background_rgba_color ArgumentError' do
181-
it 'raises ArgumentError with message' do
182-
browser.go_to
183-
expect {
184-
browser.screenshot(path: file, background_rgba_color: background_rgba_color)
185-
}.to raise_exception(ArgumentError, 'Specify :background_rgba_color as [R,G,B,A] array')
186-
end
187-
end
188-
189-
include_examples 'background_rgba_color ArgumentError' do
190-
let(:background_rgba_color) do
191-
r, g, b = 0, 0, 0
192-
[r, g, b]
193-
end
194-
end
195-
196-
include_examples 'background_rgba_color ArgumentError' do
197-
let(:background_rgba_color) { '#FFF' }
198-
end
199-
200-
include_examples 'background_rgba_color ArgumentError' do
201-
let(:background_rgba_color) { %w[0 0 0 0] }
202-
end
203-
180+
describe 'background_color option' do
204181
it 'supports screenshotting page with the specific background color' do
205182
begin
206183
file = PROJECT_ROOT + "/spec/tmp/screenshot.jpeg"
207184
browser.go_to
208185
browser.screenshot(path: file)
209186
content = File.read(file)
210-
browser.screenshot(path: file, background_rgba_color: [0, 0, 0, 0.0])
187+
browser.screenshot(path: file, background_color: RGBA.new(0, 0, 0, 0.0))
211188
content_with_specific_bc = File.read(file)
212189
expect(content).not_to eq(content_with_specific_bc)
213190
ensure
214191
FileUtils.rm_f([file])
215192
end
216193
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
217201
end
218202

219203
shared_examples "when scale is set" do

0 commit comments

Comments
 (0)