From 195f0de060432c3efb0a27d0006d0f46fed25846 Mon Sep 17 00:00:00 2001 From: AmirHdm Date: Thu, 15 Jun 2023 00:38:23 +0100 Subject: [PATCH 1/5] Replace index to by timestamp option based on YY-MM-DD_HH-MM-SS into _get_screenshot_path /* def _get_screenshot_path(self, filename): if self._screenshot_root_directory != EMBED: directory = self._screenshot_root_directory or self.log_dir else: directory = self.log_dir filename = filename.replace("/", os.sep) YY_MM_DD_HH_MM_SS = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') while True: YY_MM_DD_HH_MM_SS = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') formatted = _format_path(filename, YY_MM_DD_HH_MM_SS) path = os.path.join(directory, formatted) # filename didn't contain {YY_MM_DD_HH_MM_SS} or unique path was found if formatted == filename or not os.path.exists(path): return path */ This action is inprogress --- src/SeleniumLibrary/keywords/screenshot.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index 628361c8a..e6cedec7c 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import os +import datetime from typing import Union from robot.utils import get_link_path @@ -22,7 +23,8 @@ from SeleniumLibrary.base import LibraryComponent, keyword from SeleniumLibrary.utils.path_formatter import _format_path -DEFAULT_FILENAME_PAGE = "selenium-screenshot-{index}.png" +#DEFAULT_FILENAME_PAGE = "selenium-screenshot-{index}.png" +DEFAULT_FILENAME_PAGE = "selenium-screenshot-{YY_MM_DD_HH_MM_SS}.png" DEFAULT_FILENAME_ELEMENT = "selenium-element-screenshot-{index}.png" EMBED = "EMBED" @@ -202,10 +204,10 @@ def _get_screenshot_path(self, filename): else: directory = self.log_dir filename = filename.replace("/", os.sep) - index = 0 + YY_MM_DD_HH_MM_SS = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') while True: - index += 1 - formatted = _format_path(filename, index) + YY_MM_DD_HH_MM_SS = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + formatted = _format_path(filename, YY_MM_DD_HH_MM_SS) 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): From 6112fa60ac6d372e71052d6e847bbd420c3f5cf1 Mon Sep 17 00:00:00 2001 From: AmirHdm Date: Thu, 15 Jun 2023 23:50:23 +0100 Subject: [PATCH 2/5] Modifiy the index for screenshot to the following structure : Selenium-Screenshot-{index} index : 'YYYY-MM-DD-HH-MM-SS-RANDOM --- src/SeleniumLibrary/__init__.pyi | 2 +- src/SeleniumLibrary/keywords/screenshot.py | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/SeleniumLibrary/__init__.pyi b/src/SeleniumLibrary/__init__.pyi index eedbad5ab..8220802c0 100644 --- a/src/SeleniumLibrary/__init__.pyi +++ b/src/SeleniumLibrary/__init__.pyi @@ -13,7 +13,7 @@ class SeleniumLibrary: def alert_should_not_be_present(self, action: str = 'ACCEPT', timeout: Optional[Optional[datetime.timedelta]] = None): ... def assign_id_to_element(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str], id: str): ... def capture_element_screenshot(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, None, str], filename: str = 'selenium-element-screenshot-{index}.png'): ... - def capture_page_screenshot(self, filename: str = 'selenium-screenshot-{index}.png'): ... + def capture_page_screenshot(self, filename: str = 'selenium-screenshot-{Y_M_D}.png'): ... def checkbox_should_be_selected(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str]): ... def checkbox_should_not_be_selected(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str]): ... def choose_file(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str], file_path: str): ... diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index e6cedec7c..2cd986db3 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -14,7 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. import os -import datetime +from datetime import datetime +import random + from typing import Union from robot.utils import get_link_path @@ -23,12 +25,12 @@ from SeleniumLibrary.base import LibraryComponent, keyword from SeleniumLibrary.utils.path_formatter import _format_path -#DEFAULT_FILENAME_PAGE = "selenium-screenshot-{index}.png" -DEFAULT_FILENAME_PAGE = "selenium-screenshot-{YY_MM_DD_HH_MM_SS}.png" +DEFAULT_FILENAME_PAGE = "selenium-screenshot-{index}.png" DEFAULT_FILENAME_ELEMENT = "selenium-element-screenshot-{index}.png" EMBED = "EMBED" + class ScreenshotKeywords(LibraryComponent): @keyword def set_screenshot_directory(self, path: Union[None, str]) -> str: @@ -204,10 +206,14 @@ def _get_screenshot_path(self, filename): else: directory = self.log_dir filename = filename.replace("/", os.sep) - YY_MM_DD_HH_MM_SS = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + lower_limit = 10**4 + upper_limit = 10**6 + random_long = random.randint(lower_limit, upper_limit) + index = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + "-"+ str(random_long) while True: - YY_MM_DD_HH_MM_SS = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') - formatted = _format_path(filename, YY_MM_DD_HH_MM_SS) + random_long = random.randint(lower_limit, upper_limit) + index = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + "-"+ str(random_long) + 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): From 6f404173ddab06b7e85d306b4f1b9f1d0aff2889 Mon Sep 17 00:00:00 2001 From: AmirHdm Date: Thu, 15 Jun 2023 23:58:45 +0100 Subject: [PATCH 3/5] Fix Index name --- src/SeleniumLibrary/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SeleniumLibrary/__init__.pyi b/src/SeleniumLibrary/__init__.pyi index 8220802c0..eedbad5ab 100644 --- a/src/SeleniumLibrary/__init__.pyi +++ b/src/SeleniumLibrary/__init__.pyi @@ -13,7 +13,7 @@ class SeleniumLibrary: def alert_should_not_be_present(self, action: str = 'ACCEPT', timeout: Optional[Optional[datetime.timedelta]] = None): ... def assign_id_to_element(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str], id: str): ... def capture_element_screenshot(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, None, str], filename: str = 'selenium-element-screenshot-{index}.png'): ... - def capture_page_screenshot(self, filename: str = 'selenium-screenshot-{Y_M_D}.png'): ... + def capture_page_screenshot(self, filename: str = 'selenium-screenshot-{index}.png'): ... def checkbox_should_be_selected(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str]): ... def checkbox_should_not_be_selected(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str]): ... def choose_file(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str], file_path: str): ... From d5b888c2659b3fe06a9b0637054aa50d5d81cc5d Mon Sep 17 00:00:00 2001 From: AmirHdm Date: Wed, 28 Jun 2023 23:54:12 +0100 Subject: [PATCH 4/5] Add indexing with timestamp or a numeriacl randon for page capture feature Add formatting functions for a randon & timestamp placeholders --- src/SeleniumLibrary/__init__.pyi | 3 +- src/SeleniumLibrary/keywords/screenshot.py | 45 +++++++++++++-------- src/SeleniumLibrary/utils/path_formatter.py | 11 +++++ 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/SeleniumLibrary/__init__.pyi b/src/SeleniumLibrary/__init__.pyi index eedbad5ab..4b3338461 100644 --- a/src/SeleniumLibrary/__init__.pyi +++ b/src/SeleniumLibrary/__init__.pyi @@ -1,4 +1,5 @@ -from datetime import timedelta +import typing +from datetime import timedelta, datetime from typing import Any, Optional, Union import selenium diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index 2cd986db3..aa2ce5ec4 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -14,24 +14,33 @@ # See the License for the specific language governing permissions and # limitations under the License. import os -from datetime import datetime +from datetime import datetime import random - from typing import Union from robot.utils import get_link_path from selenium.webdriver.remote.webelement import WebElement from SeleniumLibrary.base import LibraryComponent, keyword -from SeleniumLibrary.utils.path_formatter import _format_path +from SeleniumLibrary.utils.path_formatter import _format_pathr +from SeleniumLibrary.utils.path_formatter import _format_patht -DEFAULT_FILENAME_PAGE = "selenium-screenshot-{index}.png" +DEFAULT_FILENAME_PAGE = "selenium-screenshot-{random}.png" DEFAULT_FILENAME_ELEMENT = "selenium-element-screenshot-{index}.png" EMBED = "EMBED" +TIMESTAMP = "timestamp" +RAND = "random" +LOW_LIMIT_RANDOM = 10**4 +UPPER_LIMIT_RANDOM = 10**8 + + + + class ScreenshotKeywords(LibraryComponent): + @keyword def set_screenshot_directory(self, path: Union[None, str]) -> str: """Sets the directory for captured screenshots. @@ -205,19 +214,22 @@ def _get_screenshot_path(self, filename): directory = self._screenshot_root_directory or self.log_dir else: directory = self.log_dir - filename = filename.replace("/", os.sep) - lower_limit = 10**4 - upper_limit = 10**6 - random_long = random.randint(lower_limit, upper_limit) - index = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + "-"+ str(random_long) + filename = filename.replace("/", os.sep) + while True: - random_long = random.randint(lower_limit, upper_limit) - index = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + "-"+ str(random_long) - 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): - return path + if RAND in filename: + indexation = str(random.randint(LOW_LIMIT_RANDOM, UPPER_LIMIT_RANDOM)) + formatted = _format_pathr(filename, indexation) + path = os.path.join(directory, formatted) + if formatted == filename or not os.path.exists(path): + return path + elif TIMESTAMP in filename: + indexation = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + formatted = _format_patht(filename, indexation) + 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): + return path def _create_directory(self, path): target_dir = os.path.dirname(path) @@ -243,3 +255,4 @@ def _embed_to_log_as_file(self, path, width): f'', html=True, ) + diff --git a/src/SeleniumLibrary/utils/path_formatter.py b/src/SeleniumLibrary/utils/path_formatter.py index e199054cc..86bd5aa81 100644 --- a/src/SeleniumLibrary/utils/path_formatter.py +++ b/src/SeleniumLibrary/utils/path_formatter.py @@ -15,10 +15,21 @@ # limitations under the License. + def _format_path(file_path, index): return file_path.format_map(_SafeFormatter(index=index)) +def _format_pathr(file_path, random): + return file_path.format_map(_SafeFormatter(random=random)) + +def _format_patht(file_path, timestamp): + return file_path.format_map(_SafeFormatter(timestamp=timestamp)) + + + + + class _SafeFormatter(dict): def __missing__(self, key): return f"{{{key}}}" From 446beade978d3e55683f20bca495b09d2c7c0a65 Mon Sep 17 00:00:00 2001 From: AmirHdm Date: Thu, 29 Jun 2023 01:20:32 +0100 Subject: [PATCH 5/5] Update file name with filename: str = 'selenium-screenshot-{index} or {random} or {timestamp}.png' Add three indexing option : index / random / timestamp --- src/SeleniumLibrary/__init__.pyi | 5 ++--- src/SeleniumLibrary/keywords/screenshot.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/SeleniumLibrary/__init__.pyi b/src/SeleniumLibrary/__init__.pyi index 4b3338461..541537009 100644 --- a/src/SeleniumLibrary/__init__.pyi +++ b/src/SeleniumLibrary/__init__.pyi @@ -1,5 +1,4 @@ -import typing -from datetime import timedelta, datetime +from datetime import timedelta from typing import Any, Optional, Union import selenium @@ -14,7 +13,7 @@ class SeleniumLibrary: def alert_should_not_be_present(self, action: str = 'ACCEPT', timeout: Optional[Optional[datetime.timedelta]] = None): ... def assign_id_to_element(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str], id: str): ... def capture_element_screenshot(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, None, str], filename: str = 'selenium-element-screenshot-{index}.png'): ... - def capture_page_screenshot(self, filename: str = 'selenium-screenshot-{index}.png'): ... + def capture_page_screenshot(self, filename: str = 'selenium-screenshot-{index} or {random} or {timestamp}.png'): ... def checkbox_should_be_selected(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str]): ... def checkbox_should_not_be_selected(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str]): ... def choose_file(self, locator: Union[selenium.webdriver.remote.webelement.WebElement, str], file_path: str): ... diff --git a/src/SeleniumLibrary/keywords/screenshot.py b/src/SeleniumLibrary/keywords/screenshot.py index aa2ce5ec4..092929632 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -24,6 +24,7 @@ from SeleniumLibrary.base import LibraryComponent, keyword from SeleniumLibrary.utils.path_formatter import _format_pathr from SeleniumLibrary.utils.path_formatter import _format_patht +from SeleniumLibrary.utils.path_formatter import _format_path DEFAULT_FILENAME_PAGE = "selenium-screenshot-{random}.png" DEFAULT_FILENAME_ELEMENT = "selenium-element-screenshot-{index}.png" @@ -216,6 +217,7 @@ def _get_screenshot_path(self, filename): directory = self.log_dir filename = filename.replace("/", os.sep) + index = 0 while True: if RAND in filename: indexation = str(random.randint(LOW_LIMIT_RANDOM, UPPER_LIMIT_RANDOM)) @@ -230,7 +232,14 @@ def _get_screenshot_path(self, filename): # filename didn't contain {index} or unique path was found if formatted == filename or not os.path.exists(path): return path - + elif 'index' in filename: + 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): + return path + def _create_directory(self, path): target_dir = os.path.dirname(path) if not os.path.exists(target_dir):