Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/print_options.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
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'
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, :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
# Gets the current page size.
61
#
62
# @return [Hash] The current page size hash with :width and :height.
63
attr_reader :page_size
64
65
# Sets the page size. Can be a predefined symbol or custom size hash.
66
#
67
# @param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash.
68
def page_size=(value)
69
predefined_sizes = {
70
letter: {width: 21.59, height: 27.94},
71
legal: {width: 21.59, height: 35.56},
72
a4: {width: 21.0, height: 29.7},
73
tabloid: {width: 27.94, height: 43.18}
74
}
75
76
case value
77
when Symbol
78
raise ArgumentError, "Invalid page size: #{value}" unless predefined_sizes.key?(value)
79
80
@page_size = predefined_sizes[value]
81
when Hash
82
unless value.key?(:width) && value.key?(:height)
83
raise ArgumentError, 'Custom page size must include :width and :height'
84
end
85
86
@page_size = value
87
else
88
raise ArgumentError, 'Page size must be a Symbol or a Hash'
89
end
90
end
91
end
92
end
93
end
94
95