From 92570f03eeff9473e9c51befad982e337801dee7 Mon Sep 17 00:00:00 2001 From: Cameron Shimmin Date: Wed, 15 May 2024 10:30:33 -0700 Subject: [PATCH] [#519] FIX: Fix path of screenshot and pagesource (#525) * Update the configuration to log the screenshot and page source file path correctly for all OS types * prefix variable added. formatted prefix as 'file://' --------- Co-authored-by: Edale Miguel --- selene/core/configuration.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/selene/core/configuration.py b/selene/core/configuration.py index 54b6ede0..6ad67e69 100644 --- a/selene/core/configuration.py +++ b/selene/core/configuration.py @@ -1390,6 +1390,16 @@ def with_(self, **options_to_override) -> Config: ) return persistent.replace(self, **options) + def _format_path_as_uri(self, path): + """Converts a local file path to a URI that can be clicked in most editors and browsers.""" + prefix = 'file://' + if os.name == 'nt': # Windows specific + # Replace backslashes with forward slashes and prepend with 'file://' + return f"{prefix}{path.replace(os.sep, '/')}" + else: + # Unix-based paths + return f"{prefix}{path}" + def _generate_filename(self, prefix='', suffix=''): path = self.reports_folder next_id = next(self._counter) @@ -1409,10 +1419,11 @@ def _inject_screenshot_and_page_source_pre_hooks(self, hook): # or refactor somehow to eliminate all times defining hook fns def save_and_log_screenshot(error: TimeoutException) -> Exception: path = self._save_screenshot_strategy(self) # type: ignore + uri = self._format_path_as_uri(path) return TimeoutException( error.msg + f''' -Screenshot: file://{path}''' +Screenshot: {uri}''' ) def save_and_log_page_source(error: TimeoutException) -> Exception: @@ -1425,7 +1436,8 @@ def save_and_log_page_source(error: TimeoutException) -> Exception: ) path = self._save_page_source_strategy(self, filename) - return TimeoutException(error.msg + f'\nPageSource: file://{path}') + uri = self._format_path_as_uri(path) + return TimeoutException(error.msg + f'\nPageSource: {uri}') return fp.pipe( save_and_log_screenshot if self.save_screenshot_on_failure else None,