Skip to content

Commit fddae90

Browse files
authored
[rb] Add PrintOptions Implementation for Ruby WebDriver (#15158)
1 parent e8fae63 commit fddae90

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
21+
module Selenium
22+
module WebDriver
23+
# Represents options for printing a page.
24+
class PrintOptions
25+
DEFAULT_SCALE = 1.0
26+
DEFAULT_ORIENTATION = 'portrait'
27+
DEFAULT_PAGE_SIZE = {width: 21.0, height: 29.7}.freeze # A4 size in cm
28+
DEFAULT_MARGINS = {top: 1.0, bottom: 1.0, left: 1.0, right: 1.0}.freeze
29+
30+
attr_accessor :orientation, :scale, :background, :page_ranges, :margins
31+
32+
def initialize
33+
@orientation = DEFAULT_ORIENTATION
34+
@scale = DEFAULT_SCALE
35+
@background = false
36+
@page_ranges = nil
37+
@page_size = DEFAULT_PAGE_SIZE
38+
@margins = DEFAULT_MARGINS
39+
end
40+
41+
# Converts the options to a hash format to be used by WebDriver.
42+
#
43+
# @return [Hash]
44+
def to_h
45+
options = {
46+
orientation: @orientation,
47+
scale: @scale,
48+
background: @background,
49+
pageRanges: @page_ranges,
50+
paperWidth: @page_size[:width],
51+
paperHeight: @page_size[:height],
52+
marginTop: @margins[:top],
53+
marginBottom: @margins[:bottom],
54+
marginLeft: @margins[:left],
55+
marginRight: @margins[:right]
56+
}
57+
58+
options.compact
59+
end
60+
61+
# Gets the current page size.
62+
#
63+
# @return [Hash] The current page size hash with :width and :height.
64+
attr_reader :page_size
65+
66+
# Sets the page size. Can be a predefined symbol or custom size hash.
67+
#
68+
# @param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash.
69+
def page_size=(value)
70+
predefined_sizes = {
71+
letter: {width: 21.59, height: 27.94},
72+
legal: {width: 21.59, height: 35.56},
73+
a4: {width: 21.0, height: 29.7},
74+
tabloid: {width: 27.94, height: 43.18}
75+
}
76+
77+
case value
78+
when Symbol
79+
raise ArgumentError, "Invalid page size: #{value}" unless predefined_sizes.key?(value)
80+
81+
@page_size = predefined_sizes[value]
82+
when Hash
83+
unless value.key?(:width) && value.key?(:height)
84+
raise ArgumentError, 'Custom page size must include :width and :height'
85+
end
86+
87+
@page_size = value
88+
else
89+
raise ArgumentError, 'Page size must be a Symbol or a Hash'
90+
end
91+
end
92+
end
93+
end
94+
end
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
21+
require File.expand_path('../spec_helper', __dir__)
22+
require 'selenium/webdriver/common/print_options'
23+
24+
25+
module Selenium
26+
module WebDriver
27+
describe PrintOptions do
28+
let(:options) { PrintOptions.new }
29+
30+
it 'has default values' do
31+
expect(options.orientation).to eq('portrait')
32+
expect(options.scale).to eq(1.0)
33+
expect(options.background).to be(false)
34+
expect(options.page_size).to eq({ width: 21.0, height: 29.7 })
35+
expect(options.margins).to eq({ top: 1.0, bottom: 1.0, left: 1.0, right: 1.0 })
36+
end
37+
38+
it 'can set custom page size' do
39+
custom_size = { width: 25.0, height: 30.0 }
40+
options.page_size = custom_size
41+
expect(options.page_size).to eq(custom_size)
42+
end
43+
44+
it 'can set predefined page sizes using symbols' do
45+
options.page_size = :a4
46+
expect(options.page_size).to eq({ width: 21.0, height: 29.7 })
47+
48+
options.page_size = :legal
49+
expect(options.page_size).to eq({ width: 21.59, height: 35.56 })
50+
51+
options.page_size = :tabloid
52+
expect(options.page_size).to eq({ width: 27.94, height: 43.18 })
53+
54+
options.page_size = :letter
55+
expect(options.page_size).to eq({ width: 21.59, height: 27.94 })
56+
end
57+
58+
it 'raises an error for unsupported predefined page size symbols' do
59+
expect { options.page_size = :invalid }.to raise_error(ArgumentError, /Invalid page size/)
60+
end
61+
62+
it 'can convert to a hash' do
63+
options.scale = 0.5
64+
options.background = true
65+
options.page_ranges = '1-3'
66+
hash = options.to_h
67+
68+
expect(hash).to eq(
69+
{
70+
orientation: 'portrait',
71+
scale: 0.5,
72+
background: true,
73+
pageRanges: '1-3',
74+
paperWidth: 21.0,
75+
paperHeight: 29.7,
76+
marginTop: 1.0,
77+
marginBottom: 1.0,
78+
marginLeft: 1.0,
79+
marginRight: 1.0
80+
}
81+
)
82+
end
83+
end
84+
end
85+
end

0 commit comments

Comments
 (0)