From 85243c28f734a33f2fa40c89cf75223bb7638bdd Mon Sep 17 00:00:00 2001 From: vamsi Date: Sun, 15 Nov 2026 18:36:02 -0500 Subject: [PATCH 1/2] Fix screenshot infinite loop with escaped index braces --- atest/acceptance/keywords/screenshots.robot | 28 +++++++++++++++++++++ src/SeleniumLibrary/keywords/screenshot.py | 15 ++++++----- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/atest/acceptance/keywords/screenshots.robot b/atest/acceptance/keywords/screenshots.robot index 49165bf20..741058e67 100644 --- a/atest/acceptance/keywords/screenshots.robot +++ b/atest/acceptance/keywords/screenshots.robot @@ -74,12 +74,40 @@ Capture page screenshot with index formatting File Should Exist ${OUTPUTDIR}/screenshot-and-index/format-000002-name.png Capture page screenshot with escaped braces + [Timeout] 15 seconds + [Setup] Remove Files ${OUTPUTDIR}/screenshot-and-index/brackets-{index}-name.png ${file} = Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/brackets-{{index}}-name.png File Should Exist ${OUTPUTDIR}/screenshot-and-index/brackets-{index}-name.png + ${file} = Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/brackets-{{index}}-name.png Should Be Equal ${file} ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{index}-name.png ${file} = Capture Page Screenshot ${OUTPUTDIR}/screenshot-and-index/brackets-{{index-name.png File Should Exist ${OUTPUTDIR}/screenshot-and-index/brackets-{index-name.png +Capture element screenshot with escaped braces + [Timeout] 15 seconds + [Setup] Remove Files ${OUTPUTDIR}/screenshot-and-index/element-{index}.png + ${filename} = Set Variable ${OUTPUTDIR}/screenshot-and-index/element-{{index}}.png + ${expected} = Normalize Path ${OUTPUTDIR}/screenshot-and-index/element-{index}.png + ${first} = Capture Element Screenshot tag:body ${filename} + File Should Exist ${expected} + ${second} = Capture Element Screenshot tag:body ${filename} + Should Be Equal ${first} ${expected} + Should Be Equal ${second} ${expected} + File Should Exist ${second} + +Capture page screenshot with escaped braces and unique index + [Timeout] 15 seconds + [Setup] Remove Files ${OUTPUTDIR}/screenshot-and-index/mixed-*.png + ${filename} = Set Variable ${OUTPUTDIR}/screenshot-and-index/mixed-{{index}}-{index:03}.png + ${first} = Capture Page Screenshot ${filename} + ${second} = Capture Page Screenshot ${filename} + ${expected_first} = Normalize Path ${OUTPUTDIR}/screenshot-and-index/mixed-{index}-001.png + ${expected_second} = Normalize Path ${OUTPUTDIR}/screenshot-and-index/mixed-{index}-002.png + Should Be Equal ${first} ${expected_first} + Should Be Equal ${second} ${expected_second} + File Should Exist ${first} + File Should Exist ${second} + Capture page screenshot computed name is unique [Setup] Run Keywords ... Remove files ${OUTPUTDIR}/unique-screenshot-*.png diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index 58854edff..b3e01c450 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -231,14 +231,17 @@ def _get_screenshot_path(self, filename): else: directory = self.log_dir filename = filename.replace("/", os.sep) - index = 0 + + formatted = _format_path(filename, 1) + if formatted == _format_path(filename, 2): + return os.path.join(directory, formatted) + + index = 1 while True: - index += 1 - formatted = _format_path(filename, index) - path = os.path.join(directory, formatted) - # filename didn't contain {index} or unique path was found - if formatted == filename or not os.path.exists(path): + path = os.path.join(directory, _format_path(filename, index)) + if not os.path.exists(path): return path + index += 1 def _create_directory(self, path): target_dir = os.path.dirname(path) From 86732e21371e87cc4787c69502a1379844b67e9c Mon Sep 17 00:00:00 2001 From: vamsi Date: Sun, 15 Nov 2026 18:52:47 -0500 Subject: [PATCH 2/2] Track index usage to preserve nested filename formatting --- src/SeleniumLibrary/keywords/screenshot.py | 12 +++++------- src/SeleniumLibrary/utils/path_formatter.py | 17 ++++++++++++++++- utest/test/keywords/test_screen_shot.py | 13 +++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index b3e01c450..66f22fe6f 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -21,7 +21,7 @@ from selenium.webdriver.common.print_page_options import Orientation, PrintOptions from SeleniumLibrary.base import LibraryComponent, keyword -from SeleniumLibrary.utils.path_formatter import _format_path +from SeleniumLibrary.utils.path_formatter import _format_path, _format_path_with_index from SeleniumLibrary.utils.types import Locator DEFAULT_FILENAME_PAGE = "selenium-screenshot-{index}.png" @@ -232,16 +232,14 @@ def _get_screenshot_path(self, filename): directory = self.log_dir filename = filename.replace("/", os.sep) - formatted = _format_path(filename, 1) - if formatted == _format_path(filename, 2): - return os.path.join(directory, formatted) - index = 1 + formatted, index_used = _format_path_with_index(filename, index) while True: - path = os.path.join(directory, _format_path(filename, index)) - if not os.path.exists(path): + path = os.path.join(directory, formatted) + if not index_used or not os.path.exists(path): return path index += 1 + formatted = _format_path(filename, index) def _create_directory(self, path): target_dir = os.path.dirname(path) diff --git a/src/SeleniumLibrary/utils/path_formatter.py b/src/SeleniumLibrary/utils/path_formatter.py index e199054cc..86b4cea02 100644 --- a/src/SeleniumLibrary/utils/path_formatter.py +++ b/src/SeleniumLibrary/utils/path_formatter.py @@ -16,9 +16,24 @@ def _format_path(file_path, index): - return file_path.format_map(_SafeFormatter(index=index)) + return _format_path_with_index(file_path, index)[0] + + +def _format_path_with_index(file_path, index): + formatter = _SafeFormatter(index=index) + formatted = file_path.format_map(formatter) + return formatted, formatter.index_used class _SafeFormatter(dict): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.index_used = False + + def __getitem__(self, key): + if key == "index": + self.index_used = True + return super().__getitem__(key) + def __missing__(self, key): return f"{{{key}}}" diff --git a/utest/test/keywords/test_screen_shot.py b/utest/test/keywords/test_screen_shot.py index 128d98230..52d0486f0 100644 --- a/utest/test/keywords/test_screen_shot.py +++ b/utest/test/keywords/test_screen_shot.py @@ -103,3 +103,16 @@ def test_sl_set_screenshot_directory(): cur_dir = dirname(abspath(__file__)) sl.set_screenshot_directory(cur_dir) assert sl.screenshot_root_directory == cur_dir + + +def test_screenshot_path_with_nested_index_preserves_existing_file(tmp_path): + existing = tmp_path / "shot-{other}.png" + existing.write_bytes(b"existing screenshot") + keywords = ScreenshotKeywords( + SeleniumLibrary(screenshot_root_directory=str(tmp_path)) + ) + + path = keywords._get_screenshot_path("shot-{other:>{index}}.png") + + assert path == str(tmp_path / "shot- {other}.png") + assert existing.read_bytes() == b"existing screenshot"