diff --git a/src/SeleniumLibrary/__init__.pyi b/src/SeleniumLibrary/__init__.pyi index eedbad5ab..541537009 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-{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 628361c8a..092929632 100644 --- a/src/SeleniumLibrary/keywords/screenshot.py +++ b/src/SeleniumLibrary/keywords/screenshot.py @@ -14,20 +14,34 @@ # See the License for the specific language governing permissions and # limitations under the License. import os +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_pathr +from SeleniumLibrary.utils.path_formatter import _format_patht from SeleniumLibrary.utils.path_formatter import _format_path -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. @@ -201,16 +215,31 @@ 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) + filename = filename.replace("/", os.sep) + index = 0 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): - 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 + 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): @@ -235,3 +264,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}}}"