@@ -6828,6 +6828,19 @@ def is_downloaded_file_present(self, file, browser=False):
6828
6828
self.get_path_of_downloaded_file(file, browser=browser)
6829
6829
)
6830
6830
6831
+ def is_downloaded_file_regex_present(self, regex, browser=False):
6832
+ """Returns True if the filename regex exists in the [Downloads Folder].
6833
+ Uses Python regex via the "re" library for string-matching on the name.
6834
+ @Params
6835
+ regex - The filename regex of the downloaded file.
6836
+ browser - If True, uses the path set by click-initiated downloads.
6837
+ If False, uses the self.download_file(file_url) path.
6838
+ Those paths are often the same. (browser-dependent)
6839
+ (Default: False)."""
6840
+ df = self.get_downloads_folder()
6841
+ matches = [fn for fn in os.listdir(df) if re.match(regex, fn)]
6842
+ return len(matches) >= 1
6843
+
6831
6844
def delete_downloaded_file_if_present(self, file, browser=False):
6832
6845
"""Deletes the file from the [Downloads Folder] if the file exists.
6833
6846
For browser click-initiated downloads, SeleniumBase will override
@@ -6930,6 +6943,60 @@ def assert_downloaded_file(self, file, timeout=None, browser=False):
6930
6943
except Exception:
6931
6944
pass
6932
6945
6946
+ def assert_downloaded_file_regex(self, regex, timeout=None, browser=False):
6947
+ """Assert the filename regex exists in SeleniumBase's Downloads Folder.
6948
+ Uses Python regex via the "re" library for string-matching on the name.
6949
+ @Params
6950
+ regex - The filename regex of the downloaded file.
6951
+ timeout - The time (seconds) to wait for the download to complete.
6952
+ browser - If True, uses the path set by click-initiated downloads.
6953
+ If False, uses the self.download_file(file_url) path.
6954
+ Those paths are often the same. (browser-dependent)
6955
+ (Default: False)."""
6956
+ self.__check_scope()
6957
+ if not timeout:
6958
+ timeout = settings.LARGE_TIMEOUT
6959
+ if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
6960
+ timeout = self.__get_new_timeout(timeout)
6961
+ start_ms = time.time() * 1000.0
6962
+ stop_ms = start_ms + (timeout * 1000.0)
6963
+ found = False
6964
+ df = self.get_downloads_folder()
6965
+ for x in range(int(timeout)):
6966
+ shared_utils.check_if_time_limit_exceeded()
6967
+ try:
6968
+ matches = [fn for fn in os.listdir(df) if re.match(regex, fn)]
6969
+ self.assertTrue(
6970
+ len(matches) >= 1,
6971
+ "Regex [%s] was not found in the downloads folder [%s]!"
6972
+ % (regex, self.get_downloads_folder()),
6973
+ )
6974
+ found = True
6975
+ break
6976
+ except Exception:
6977
+ now_ms = time.time() * 1000.0
6978
+ if now_ms >= stop_ms:
6979
+ break
6980
+ time.sleep(1)
6981
+ if not found:
6982
+ message = (
6983
+ "Regex {%s} was not found in the downloads folder {%s} "
6984
+ "after %s seconds! (Or the download didn't complete!)"
6985
+ % (regex, self.get_downloads_folder(), timeout)
6986
+ )
6987
+ page_actions.timeout_exception("NoSuchFileException", message)
6988
+ if self.demo_mode:
6989
+ messenger_post = (
6990
+ "<b>ASSERT DOWNLOADED FILE REGEX</b>: [%s]" % regex
6991
+ )
6992
+ try:
6993
+ js_utils.activate_jquery(self.driver)
6994
+ js_utils.post_messenger_success_message(
6995
+ self.driver, messenger_post, self.message_duration
6996
+ )
6997
+ except Exception:
6998
+ pass
6999
+
6933
7000
def assert_true(self, expr, msg=None):
6934
7001
"""Asserts that the expression is True.
6935
7002
Will raise an exception if the statement if False."""
0 commit comments