Skip to content

Commit 4ca0796

Browse files
committed
Support custom and predefined page sizes in PrintOptions; update tests
1 parent 6bd449d commit 4ca0796

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

rb/lib/selenium/webdriver/common/print_options.rb

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# <copyright file="print_options.rb" company="Selenium Committers">
24
# Licensed to the Software Freedom Conservancy (SFC) under one
35
# or more contributor license agreements. See the NOTICE file
@@ -22,11 +24,11 @@ module WebDriver
2224
# Represents options for printing a page.
2325
class PrintOptions
2426
DEFAULT_SCALE = 1.0
25-
DEFAULT_ORIENTATION = 'portrait'.freeze
27+
DEFAULT_ORIENTATION = 'portrait'
2628
DEFAULT_PAGE_SIZE = {width: 21.0, height: 29.7}.freeze # A4 size in cm
2729
DEFAULT_MARGINS = {top: 1.0, bottom: 1.0, left: 1.0, right: 1.0}.freeze
2830

29-
attr_accessor :orientation, :scale, :background, :page_ranges, :page_size, :margins
31+
attr_accessor :orientation, :scale, :background, :page_ranges, :margins
3032

3133
def initialize
3234
@orientation = DEFAULT_ORIENTATION
@@ -57,20 +59,65 @@ def to_h
5759
options.compact
5860
end
5961

60-
# Sets the page size to a predefined size.
62+
# Gets the current page size.
6163
#
62-
# @param [Symbol] size The predefined size (:letter, :legal, :a4, :tabloid).
63-
def set_page_size(size)
64+
# @return [Hash] The current page size hash with :width and :height.
65+
def page_size
66+
@page_size
67+
end
68+
69+
# Sets the page size. Can be a predefined symbol or custom size hash.
70+
#
71+
# @param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash.
72+
def page_size=(value)
6473
predefined_sizes = {
6574
letter: {width: 21.59, height: 27.94},
6675
legal: {width: 21.59, height: 35.56},
6776
a4: {width: 21.0, height: 29.7},
6877
tabloid: {width: 27.94, height: 43.18}
6978
}
7079

71-
raise ArgumentError, "Invalid page size: #{size}" unless predefined_sizes.key?(size)
80+
case value
81+
when Symbol
82+
raise ArgumentError, "Invalid page size: #{value}" unless predefined_sizes.key?(value)
83+
84+
@page_size = predefined_sizes[value]
85+
when Hash
86+
unless value.key?(:width) && value.key?(:height)
87+
raise ArgumentError, 'Custom page size must include :width and :height'
88+
end
89+
90+
@page_size = value
91+
else
92+
raise ArgumentError, 'Page size must be a Symbol or a Hash'
93+
end
94+
end
95+
96+
# Sets the page size. Can be a predefined symbol or custom size hash.
97+
#
98+
# @param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash.
99+
def page_size=(value)
100+
predefined_sizes = {
101+
letter: {width: 21.59, height: 27.94},
102+
legal: {width: 21.59, height: 35.56},
103+
a4: {width: 21.0, height: 29.7},
104+
tabloid: {width: 27.94, height: 43.18}
105+
}
106+
107+
case value
108+
when Symbol
109+
raise ArgumentError, "Invalid page size: #{value}" unless predefined_sizes.key?(value)
110+
111+
@page_size = predefined_sizes[value]
112+
when Hash
113+
unless value.key?(:width) && value.key?(:height)
114+
raise ArgumentError, 'Custom page size must include :width and :height'
115+
end
72116

73-
@page_size = predefined_sizes[size]
117+
@page_size = value
118+
else
119+
raise ArgumentError, 'Page size must be a Symbol or a Hash'
120+
end
74121
end
75122
end
76123
end

rb/spec/unit/selenium/print_options_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ module WebDriver
4040
expect(options.page_size).to eq(custom_size)
4141
end
4242

43-
it 'can set predefined page sizes' do
44-
options.set_page_size(:a4)
43+
it 'can set predefined page sizes using symbols' do
44+
options.page_size = :a4
4545
expect(options.page_size).to eq({ width: 21.0, height: 29.7 })
46-
47-
options.set_page_size(:legal)
46+
47+
options.page_size = :legal
4848
expect(options.page_size).to eq({ width: 21.59, height: 35.56 })
49-
50-
options.set_page_size(:tabloid)
49+
50+
options.page_size = :tabloid
5151
expect(options.page_size).to eq({ width: 27.94, height: 43.18 })
52-
53-
options.set_page_size(:letter)
52+
53+
options.page_size = :letter
5454
expect(options.page_size).to eq({ width: 21.59, height: 27.94 })
55-
end
55+
end
5656

57-
it 'raises an error for invalid page size' do
58-
expect { options.set_page_size(:invalid) }.to raise_error(ArgumentError, /Invalid page size/)
57+
it 'raises an error for unsupported predefined page size symbols' do
58+
expect { options.page_size = :invalid }.to raise_error(ArgumentError, /Invalid page size/)
5959
end
6060

6161
it 'can convert to a hash' do

0 commit comments

Comments
 (0)