Path: blob/trunk/rb/lib/selenium/webdriver/common/print_options.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21# Represents options for printing a page.22class PrintOptions23DEFAULT_SCALE = 1.024DEFAULT_ORIENTATION = 'portrait'25DEFAULT_PAGE_SIZE = {width: 21.0, height: 29.7}.freeze # A4 size in cm26DEFAULT_MARGINS = {top: 1.0, bottom: 1.0, left: 1.0, right: 1.0}.freeze2728attr_accessor :orientation, :scale, :background, :page_ranges, :margins2930def initialize31@orientation = DEFAULT_ORIENTATION32@scale = DEFAULT_SCALE33@background = false34@page_ranges = nil35@page_size = DEFAULT_PAGE_SIZE36@margins = DEFAULT_MARGINS37end3839# Converts the options to a hash format to be used by WebDriver.40#41# @return [Hash]42def to_h43options = {44orientation: @orientation,45scale: @scale,46background: @background,47pageRanges: @page_ranges,48paperWidth: @page_size[:width],49paperHeight: @page_size[:height],50marginTop: @margins[:top],51marginBottom: @margins[:bottom],52marginLeft: @margins[:left],53marginRight: @margins[:right]54}5556options.compact57end5859# Gets the current page size.60#61# @return [Hash] The current page size hash with :width and :height.62attr_reader :page_size6364# Sets the page size. Can be a predefined symbol or custom size hash.65#66# @param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash.67def page_size=(value)68predefined_sizes = {69letter: {width: 21.59, height: 27.94},70legal: {width: 21.59, height: 35.56},71a4: {width: 21.0, height: 29.7},72tabloid: {width: 27.94, height: 43.18}73}7475case value76when Symbol77raise ArgumentError, "Invalid page size: #{value}" unless predefined_sizes.key?(value)7879@page_size = predefined_sizes[value]80when Hash81unless value.key?(:width) && value.key?(:height)82raise ArgumentError, 'Custom page size must include :width and :height'83end8485@page_size = value86else87raise ArgumentError, 'Page size must be a Symbol or a Hash'88end89end90end91end92end939495