Skip to content

Commit abcd21d

Browse files
committed
Add methods for verifying downloads via regex
1 parent 121aade commit abcd21d

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

help_docs/method_summary.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,15 @@ self.get_path_of_downloaded_file(file, browser=False)
471471

472472
self.is_downloaded_file_present(file, browser=False)
473473

474+
self.is_downloaded_file_regex_present(regex, browser=False)
475+
474476
self.delete_downloaded_file_if_present(file, browser=False)
475477
# Duplicates: self.delete_downloaded_file(file, browser=False)
476478

477479
self.assert_downloaded_file(file, timeout=None, browser=False)
478480

481+
self.assert_downloaded_file_regex(regex, timeout=None, browser=False)
482+
479483
self.assert_true(expr, msg=None)
480484

481485
self.assert_false(expr, msg=None)

seleniumbase/fixtures/base_case.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6828,6 +6828,19 @@ def is_downloaded_file_present(self, file, browser=False):
68286828
self.get_path_of_downloaded_file(file, browser=browser)
68296829
)
68306830

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+
68316844
def delete_downloaded_file_if_present(self, file, browser=False):
68326845
"""Deletes the file from the [Downloads Folder] if the file exists.
68336846
For browser click-initiated downloads, SeleniumBase will override
@@ -6930,6 +6943,60 @@ def assert_downloaded_file(self, file, timeout=None, browser=False):
69306943
except Exception:
69316944
pass
69326945

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+
69337000
def assert_true(self, expr, msg=None):
69347001
"""Asserts that the expression is True.
69357002
Will raise an exception if the statement if False."""

0 commit comments

Comments
 (0)