Skip to content

Commit 6bd449d

Browse files
committed
[rb] Add PrintOptions class with support for predefined and custom page sizes
1 parent 997a338 commit 6bd449d

File tree

5 files changed

+81
-95
lines changed

5 files changed

+81
-95
lines changed

rb/lib/selenium/webdriver/common.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
require 'selenium/webdriver/common/driver_extensions/has_launching'
8888
require 'selenium/webdriver/common/driver_extensions/has_fedcm_dialog'
8989
require 'selenium/webdriver/common/keys'
90+
require 'selenium/webdriver/common/print_options'
9091
require 'selenium/webdriver/common/profile_helper'
9192
require 'selenium/webdriver/common/options'
9293
require 'selenium/webdriver/common/takes_screenshot'
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# <copyright file="print_options.rb" company="Selenium Committers">
2+
# Licensed to the Software Freedom Conservancy (SFC) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The SFC licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
# </copyright>
19+
20+
module Selenium
21+
module WebDriver
22+
# Represents options for printing a page.
23+
class PrintOptions
24+
DEFAULT_SCALE = 1.0
25+
DEFAULT_ORIENTATION = 'portrait'.freeze
26+
DEFAULT_PAGE_SIZE = {width: 21.0, height: 29.7}.freeze # A4 size in cm
27+
DEFAULT_MARGINS = {top: 1.0, bottom: 1.0, left: 1.0, right: 1.0}.freeze
28+
29+
attr_accessor :orientation, :scale, :background, :page_ranges, :page_size, :margins
30+
31+
def initialize
32+
@orientation = DEFAULT_ORIENTATION
33+
@scale = DEFAULT_SCALE
34+
@background = false
35+
@page_ranges = nil
36+
@page_size = DEFAULT_PAGE_SIZE
37+
@margins = DEFAULT_MARGINS
38+
end
39+
40+
# Converts the options to a hash format to be used by WebDriver.
41+
#
42+
# @return [Hash]
43+
def to_h
44+
options = {
45+
orientation: @orientation,
46+
scale: @scale,
47+
background: @background,
48+
pageRanges: @page_ranges,
49+
paperWidth: @page_size[:width],
50+
paperHeight: @page_size[:height],
51+
marginTop: @margins[:top],
52+
marginBottom: @margins[:bottom],
53+
marginLeft: @margins[:left],
54+
marginRight: @margins[:right]
55+
}
56+
57+
options.compact
58+
end
59+
60+
# Sets the page size to a predefined size.
61+
#
62+
# @param [Symbol] size The predefined size (:letter, :legal, :a4, :tabloid).
63+
def set_page_size(size)
64+
predefined_sizes = {
65+
letter: {width: 21.59, height: 27.94},
66+
legal: {width: 21.59, height: 35.56},
67+
a4: {width: 21.0, height: 29.7},
68+
tabloid: {width: 27.94, height: 43.18}
69+
}
70+
71+
raise ArgumentError, "Invalid page size: #{size}" unless predefined_sizes.key?(size)
72+
73+
@page_size = predefined_sizes[size]
74+
end
75+
end
76+
end
77+
end

rb/lib/selenium/webdriver/print_options.rb

Lines changed: 0 additions & 78 deletions
This file was deleted.

rb/spec/spec_helper.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,2 @@
1-
require 'spec_helper'
2-
require 'selenium/webdriver/print_options'
3-
4-
RSpec.configure do |config|
5-
config.expect_with :rspec do |expectations|
6-
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
7-
end
8-
9-
config.mock_with :rspec do |mocks|
10-
mocks.verify_partial_doubles = true
11-
end
12-
13-
config.shared_context_metadata_behavior = :apply_to_host_groups
14-
end
15-
1+
$LOAD_PATH.unshift File.expand_path('../../lib', __dir__)
2+
require 'rspec'

rb/spec/unit/selenium/print_options_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717

1818
# frozen_string_literal: true
1919

20-
require 'spec_helper'
2120
require_relative '../../spec_helper'
22-
require 'selenium/webdriver/print_options'
21+
require 'selenium/webdriver/common/print_options'
2322

2423

2524
module Selenium

0 commit comments

Comments
 (0)