Skip to content

Commit

Permalink
[rb] fix bugs and allow saving print page
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Apr 8, 2021
1 parent 8dae816 commit 4162e16
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
Expand Up @@ -21,8 +21,35 @@ module Selenium
module WebDriver
module DriverExtensions
module PrintsPage
#
# Save a page as a PDF to the given path
#
# @example Save Printed Page
# driver.save_print_page('../printed_page.pdf')
#
# @param [String] path to where the pdf should be saved
#
# @api public
#

def save_print_page(path, **options)
File.open(path, 'wb') do |file|
content = Base64.decode64 print_page(options)
file << content
end
end

#
# Return a Base64 encoded Print Page as a string
#
# @see https://w3c.github.io/webdriver/#print-page
#
# @api public
#

def print_page(**options)
options[:page_ranges] &&= Array(options[:page_ranges])
options[:pageRanges] = Array(options.delete(:page_ranges)) || []
options[:shrinkToFit] = options.delete(:shrink_to_fit) { true }

@bridge.print_page(options)
end
Expand Down
6 changes: 0 additions & 6 deletions rb/lib/selenium/webdriver/remote/driver.rb
Expand Up @@ -44,12 +44,6 @@ def initialize(bridge: nil, listener: nil, **opts)
super
end

def print_page(**options)
options[:page_ranges] &&= Array(options[:page_ranges])

@bridge.print_page(options)
end

private

def devtools_address
Expand Down
22 changes: 15 additions & 7 deletions rb/spec/integration/selenium/webdriver/chrome/driver_spec.rb
Expand Up @@ -53,7 +53,7 @@ module Chrome
end
end

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

Expand All @@ -64,19 +64,27 @@ module Chrome
end
end

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

it 'should print with valid params' do
it 'should save pdf' do
create_driver!(capabilities: options) do |driver|
driver.navigate.to url_for('printPage.html')
expect(driver.print_page(orientation: 'landscape',
page_ranges: ['1-2'],
page: {width: 30})).to include(magic_number)

path = "#{Dir.tmpdir}/test#{SecureRandom.urlsafe_base64}.pdf"

driver.save_print_page path

expect(File.exist?(path)).to be true
expect(File.size(path)).to be_positive
ensure
File.delete(path) if File.exist?(path)
end
end
end
Expand Down

0 comments on commit 4162e16

Please sign in to comment.