Skip to content

[py] Enhance PrintOptions to support default, predefined, and custom page sizes (#15052) #15064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fa7626f
Enhance PrintOptions to support default, predefined, and custom page …
yvsvarma Jan 12, 2025
32d9646
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 12, 2025
d51de87
adding reference link for page sizes
yvsvarma Jan 13, 2025
35faaa3
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 15, 2025
4a27588
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 16, 2025
2846c1f
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 18, 2025
52d33a6
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 19, 2025
335fa8f
Merge branch 'trunk' into final-python-pagesize-support
VietND96 Jan 20, 2025
39047a6
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 21, 2025
30dc92d
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 22, 2025
89b629b
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 23, 2025
8cd136b
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 23, 2025
06e4336
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 25, 2025
34a7da4
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 26, 2025
99a6456
Merge branch 'trunk' into final-python-pagesize-support
VietND96 Jan 27, 2025
e0d00e1
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 27, 2025
de5b2e3
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 28, 2025
0039509
fixing format issues
yvsvarma Jan 28, 2025
c63e78b
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 30, 2025
1fe87f8
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Jan 31, 2025
715ab8b
fixing the assertion to compare with then new default width
yvsvarma Jan 31, 2025
eda2ec7
Merge branch 'trunk' into final-python-pagesize-support
yvsvarma Feb 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion py/selenium/webdriver/common/print_page_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,43 @@ class PrintOptions:
- Set
- `None`
"""
# Reference for predefined page size constants: https://www.agooddaytoprint.com/page/paper-size-chart-faq
A4 = {"height": 29.7, "width": 21.0} # size in cm
LEGAL = {"height": 35.56, "width": 21.59} # size in cm
LETTER = {"height": 27.94, "width": 21.59} # size in cm
TABLOID = {"height": 43.18, "width": 27.94} # size in cm

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

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

def set_page_size(self, page_size: dict) -> None:
"""Sets the page size to predefined or custom dimensions.

Parameters
----------
page_size: dict
A dictionary containing `height` and `width` as keys with respective values.

Example
-------
self.set_page_size(PageSize.A4) # A4 predefined size
self.set_page_size({"height": 15.0, "width": 20.0}) # Custom size in cm
"""
self._validate_num_property("height", page_size["height"])
self._validate_num_property("width", page_size["width"])
self._page["height"] = page_size["height"]
self._page["width"] = page_size["width"]
self._print_options["page"] = self._page

def _validate_num_property(self, property_name: str, value: float) -> None:
"""Helper function to validate some of the properties."""
if not isinstance(value, (int, float)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ def test_raises_exception_if_scale_is_outside_range(print_options):
print_options.scale = 3


def test_set_page_size(print_options):
# Example of setting a default (A4)
assert print_options.page_width == PrintOptions.A4["width"]
assert print_options.page_height == PrintOptions.A4["height"]

# Example of setting a predefined page size
print_options.set_page_size(PrintOptions.LEGAL)
assert print_options.page_width == PrintOptions.LEGAL["width"]
assert print_options.page_height == PrintOptions.LEGAL["height"]

# Test custom page size
custom_size = {"height": 25.0, "width": 15.0}
print_options.set_page_size(custom_size)
assert print_options.page_width == custom_size["width"]
assert print_options.page_height == custom_size["height"]


def test_raises_exception_if_scale_is_not_an_integer(print_options):
with pytest.raises(ValueError):
print_options.scale = "1"
Expand All @@ -56,7 +73,7 @@ def test_set_background(print_options):


def test_unset_value_to_be_none(print_options):
assert print_options.page_width is None
assert print_options.page_width == PrintOptions.A4["width"]


def test_set_width(print_options):
Expand Down