From fa7626f8c7ee56970f7492d29784ba6a4f4c6cfd Mon Sep 17 00:00:00 2001 From: yvsvarma Date: Sun, 12 Jan 2025 08:51:05 -0600 Subject: [PATCH 1/4] Enhance PrintOptions to support default, predefined, and custom page sizes in Python (#15052) --- .../webdriver/common/print_page_options.py | 29 ++++++++++++++++++- .../common/print_page_options_tests.py | 16 ++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/py/selenium/webdriver/common/print_page_options.py b/py/selenium/webdriver/common/print_page_options.py index 1ffde95c88e67..e0ae9b1b1c600 100644 --- a/py/selenium/webdriver/common/print_page_options.py +++ b/py/selenium/webdriver/common/print_page_options.py @@ -402,16 +402,43 @@ class PrintOptions: - Set - `None` """ + # Predefined page sizes (in centimeters) + 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)): diff --git a/py/test/unit/selenium/webdriver/common/print_page_options_tests.py b/py/test/unit/selenium/webdriver/common/print_page_options_tests.py index 76666de94f746..1c276bb165c4f 100644 --- a/py/test/unit/selenium/webdriver/common/print_page_options_tests.py +++ b/py/test/unit/selenium/webdriver/common/print_page_options_tests.py @@ -44,6 +44,22 @@ def test_raises_exception_if_scale_is_outside_range(print_options): with pytest.raises(ValueError): 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): From d51de8795eb8e77a94988933e57af53fd9fb4fa6 Mon Sep 17 00:00:00 2001 From: yvsvarma Date: Mon, 13 Jan 2025 02:45:33 -0600 Subject: [PATCH 2/4] adding reference link for page sizes --- py/selenium/webdriver/common/print_page_options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/selenium/webdriver/common/print_page_options.py b/py/selenium/webdriver/common/print_page_options.py index e0ae9b1b1c600..7e4f7d9524c97 100644 --- a/py/selenium/webdriver/common/print_page_options.py +++ b/py/selenium/webdriver/common/print_page_options.py @@ -402,7 +402,7 @@ class PrintOptions: - Set - `None` """ - # Predefined page sizes (in centimeters) + # 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 From 0039509e3c7e04a0a4ce35739179734df1957dfa Mon Sep 17 00:00:00 2001 From: yvsvarma Date: Tue, 28 Jan 2025 04:01:40 -0600 Subject: [PATCH 3/4] fixing format issues --- py/selenium/webdriver/common/print_page_options.py | 10 +++++----- .../webdriver/common/print_page_options_tests.py | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/py/selenium/webdriver/common/print_page_options.py b/py/selenium/webdriver/common/print_page_options.py index 7e4f7d9524c97..b6c3941826c6f 100644 --- a/py/selenium/webdriver/common/print_page_options.py +++ b/py/selenium/webdriver/common/print_page_options.py @@ -410,7 +410,10 @@ class PrintOptions: def __init__(self) -> None: self._print_options: _PrintOpts = {} - self._page: _PageOpts = {"height": PrintOptions.A4["height"], "width": PrintOptions.A4["width"]} # Default page size set to A4 + 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: @@ -418,8 +421,7 @@ def to_dict(self) -> _PrintOpts: return self._print_options def set_page_size(self, page_size: dict) -> None: - """ - Sets the page size to predefined or custom dimensions. + """Sets the page size to predefined or custom dimensions. Parameters ---------- @@ -430,7 +432,6 @@ def set_page_size(self, page_size: dict) -> None: ------- 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"]) @@ -438,7 +439,6 @@ def set_page_size(self, page_size: dict) -> None: 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)): diff --git a/py/test/unit/selenium/webdriver/common/print_page_options_tests.py b/py/test/unit/selenium/webdriver/common/print_page_options_tests.py index 1c276bb165c4f..de1fa604e47da 100644 --- a/py/test/unit/selenium/webdriver/common/print_page_options_tests.py +++ b/py/test/unit/selenium/webdriver/common/print_page_options_tests.py @@ -44,6 +44,7 @@ def test_raises_exception_if_scale_is_outside_range(print_options): with pytest.raises(ValueError): 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"] From 715ab8b0771af4062c579936a226b466d57d1d4f Mon Sep 17 00:00:00 2001 From: yvsvarma Date: Fri, 31 Jan 2025 08:41:03 -0600 Subject: [PATCH 4/4] fixing the assertion to compare with then new default width --- .../unit/selenium/webdriver/common/print_page_options_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/test/unit/selenium/webdriver/common/print_page_options_tests.py b/py/test/unit/selenium/webdriver/common/print_page_options_tests.py index de1fa604e47da..1e3f0f50683b4 100644 --- a/py/test/unit/selenium/webdriver/common/print_page_options_tests.py +++ b/py/test/unit/selenium/webdriver/common/print_page_options_tests.py @@ -73,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):