Skip to content

Commit 4162e16

Browse files
committed
[rb] fix bugs and allow saving print page
1 parent 8dae816 commit 4162e16

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

rb/lib/selenium/webdriver/common/driver_extensions/prints_page.rb

+28-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,35 @@ module Selenium
2121
module WebDriver
2222
module DriverExtensions
2323
module PrintsPage
24+
#
25+
# Save a page as a PDF to the given path
26+
#
27+
# @example Save Printed Page
28+
# driver.save_print_page('../printed_page.pdf')
29+
#
30+
# @param [String] path to where the pdf should be saved
31+
#
32+
# @api public
33+
#
34+
35+
def save_print_page(path, **options)
36+
File.open(path, 'wb') do |file|
37+
content = Base64.decode64 print_page(options)
38+
file << content
39+
end
40+
end
41+
42+
#
43+
# Return a Base64 encoded Print Page as a string
44+
#
45+
# @see https://w3c.github.io/webdriver/#print-page
46+
#
47+
# @api public
48+
#
49+
2450
def print_page(**options)
25-
options[:page_ranges] &&= Array(options[:page_ranges])
51+
options[:pageRanges] = Array(options.delete(:page_ranges)) || []
52+
options[:shrinkToFit] = options.delete(:shrink_to_fit) { true }
2653

2754
@bridge.print_page(options)
2855
end

rb/lib/selenium/webdriver/remote/driver.rb

-6
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ def initialize(bridge: nil, listener: nil, **opts)
4444
super
4545
end
4646

47-
def print_page(**options)
48-
options[:page_ranges] &&= Array(options[:page_ranges])
49-
50-
@bridge.print_page(options)
51-
end
52-
5347
private
5448

5549
def devtools_address

rb/spec/integration/selenium/webdriver/chrome/driver_spec.rb

+15-7
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module Chrome
5353
end
5454
end
5555

56-
describe '#print_options' do
56+
describe 'PrintsPage' do
5757
let(:magic_number) { 'JVBER' }
5858
let(:options) { Chrome::Options.new(args: ['--headless']) }
5959

@@ -64,19 +64,27 @@ module Chrome
6464
end
6565
end
6666

67-
it 'should print with orientation' do
67+
it 'should print with valid params' do
6868
create_driver!(capabilities: options) do |driver|
6969
driver.navigate.to url_for('printPage.html')
70-
expect(driver.print_page(orientation: 'landscape')).to include(magic_number)
70+
expect(driver.print_page(orientation: 'landscape',
71+
page_ranges: ['1-2'],
72+
page: {width: 30})).to include(magic_number)
7173
end
7274
end
7375

74-
it 'should print with valid params' do
76+
it 'should save pdf' do
7577
create_driver!(capabilities: options) do |driver|
7678
driver.navigate.to url_for('printPage.html')
77-
expect(driver.print_page(orientation: 'landscape',
78-
page_ranges: ['1-2'],
79-
page: {width: 30})).to include(magic_number)
79+
80+
path = "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.pdf"
81+
82+
driver.save_print_page path
83+
84+
expect(File.exist?(path)).to be true
85+
expect(File.size(path)).to be_positive
86+
ensure
87+
File.delete(path) if File.exist?(path)
8088
end
8189
end
8290
end

0 commit comments

Comments
 (0)