Skip to content

Commit bd2e454

Browse files
yvsvarmaVietND96
andauthored
[py] Enhance PrintOptions to support default, predefined, and custom page sizes (#15052) (#15064)
* Enhance PrintOptions to support default, predefined, and custom page sizes in Python (#15052) * adding reference link for page sizes * fixing format issues * fixing the assertion to compare with then new default width --------- Co-authored-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
1 parent 42d7dee commit bd2e454

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

py/selenium/webdriver/common/print_page_options.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,43 @@ class PrintOptions:
402402
- Set
403403
- `None`
404404
"""
405+
# Reference for predefined page size constants: https://www.agooddaytoprint.com/page/paper-size-chart-faq
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 = {
414+
"height": PrintOptions.A4["height"],
415+
"width": PrintOptions.A4["width"],
416+
} # Default page size set to A4
409417
self._margin: _MarginOpts = {}
410418

411419
def to_dict(self) -> _PrintOpts:
412420
""":Returns: A hash of print options configured."""
413421
return self._print_options
414422

423+
def set_page_size(self, page_size: dict) -> None:
424+
"""Sets the page size to predefined or custom dimensions.
425+
426+
Parameters
427+
----------
428+
page_size: dict
429+
A dictionary containing `height` and `width` as keys with respective values.
430+
431+
Example
432+
-------
433+
self.set_page_size(PageSize.A4) # A4 predefined size
434+
self.set_page_size({"height": 15.0, "width": 20.0}) # Custom size in cm
435+
"""
436+
self._validate_num_property("height", page_size["height"])
437+
self._validate_num_property("width", page_size["width"])
438+
self._page["height"] = page_size["height"]
439+
self._page["width"] = page_size["width"]
440+
self._print_options["page"] = self._page
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)):

py/test/unit/selenium/webdriver/common/print_page_options_tests.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ def test_raises_exception_if_scale_is_outside_range(print_options):
4545
print_options.scale = 3
4646

4747

48+
def test_set_page_size(print_options):
49+
# Example of setting a default (A4)
50+
assert print_options.page_width == PrintOptions.A4["width"]
51+
assert print_options.page_height == PrintOptions.A4["height"]
52+
53+
# Example of setting a predefined page size
54+
print_options.set_page_size(PrintOptions.LEGAL)
55+
assert print_options.page_width == PrintOptions.LEGAL["width"]
56+
assert print_options.page_height == PrintOptions.LEGAL["height"]
57+
58+
# Test custom page size
59+
custom_size = {"height": 25.0, "width": 15.0}
60+
print_options.set_page_size(custom_size)
61+
assert print_options.page_width == custom_size["width"]
62+
assert print_options.page_height == custom_size["height"]
63+
64+
4865
def test_raises_exception_if_scale_is_not_an_integer(print_options):
4966
with pytest.raises(ValueError):
5067
print_options.scale = "1"
@@ -56,7 +73,7 @@ def test_set_background(print_options):
5673

5774

5875
def test_unset_value_to_be_none(print_options):
59-
assert print_options.page_width is None
76+
assert print_options.page_width == PrintOptions.A4["width"]
6077

6178

6279
def test_set_width(print_options):

0 commit comments

Comments
 (0)