Skip to content

Commit fa7626f

Browse files
committed
Enhance PrintOptions to support default, predefined, and custom page sizes in Python (SeleniumHQ#15052)
1 parent 928833e commit fa7626f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Diff for: py/selenium/webdriver/common/print_page_options.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,43 @@ class PrintOptions:
402402
- Set
403403
- `None`
404404
"""
405+
# Predefined page sizes (in centimeters)
406+
A4 = {"height": 29.7, "width": 21.0} # size in cm
407+
LEGAL = {"height": 35.56, "width": 21.59} # size in cm
408+
LETTER = {"height": 27.94, "width": 21.59} # size in cm
409+
TABLOID = {"height": 43.18, "width": 27.94} # size in cm
405410

406411
def __init__(self) -> None:
407412
self._print_options: _PrintOpts = {}
408-
self._page: _PageOpts = {}
413+
self._page: _PageOpts = {"height": PrintOptions.A4["height"], "width": PrintOptions.A4["width"]} # Default page size set to A4
409414
self._margin: _MarginOpts = {}
410415

411416
def to_dict(self) -> _PrintOpts:
412417
""":Returns: A hash of print options configured."""
413418
return self._print_options
414419

420+
def set_page_size(self, page_size: dict) -> None:
421+
"""
422+
Sets the page size to predefined or custom dimensions.
423+
424+
Parameters
425+
----------
426+
page_size: dict
427+
A dictionary containing `height` and `width` as keys with respective values.
428+
429+
Example
430+
-------
431+
self.set_page_size(PageSize.A4) # A4 predefined size
432+
self.set_page_size({"height": 15.0, "width": 20.0}) # Custom size in cm
433+
434+
"""
435+
self._validate_num_property("height", page_size["height"])
436+
self._validate_num_property("width", page_size["width"])
437+
self._page["height"] = page_size["height"]
438+
self._page["width"] = page_size["width"]
439+
self._print_options["page"] = self._page
440+
441+
415442
def _validate_num_property(self, property_name: str, value: float) -> None:
416443
"""Helper function to validate some of the properties."""
417444
if not isinstance(value, (int, float)):

Diff for: py/test/unit/selenium/webdriver/common/print_page_options_tests.py

+16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ def test_raises_exception_if_scale_is_outside_range(print_options):
4444
with pytest.raises(ValueError):
4545
print_options.scale = 3
4646

47+
def test_set_page_size(print_options):
48+
# Example of setting a default (A4)
49+
assert print_options.page_width == PrintOptions.A4["width"]
50+
assert print_options.page_height == PrintOptions.A4["height"]
51+
52+
# Example of setting a predefined page size
53+
print_options.set_page_size(PrintOptions.LEGAL)
54+
assert print_options.page_width == PrintOptions.LEGAL["width"]
55+
assert print_options.page_height == PrintOptions.LEGAL["height"]
56+
57+
# Test custom page size
58+
custom_size = {"height": 25.0, "width": 15.0}
59+
print_options.set_page_size(custom_size)
60+
assert print_options.page_width == custom_size["width"]
61+
assert print_options.page_height == custom_size["height"]
62+
4763

4864
def test_raises_exception_if_scale_is_not_an_integer(print_options):
4965
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)