Skip to content

Commit 0869b11

Browse files
committed
Add a new method: "self.delete_downloaded_file_if_present(file)"
1 parent 540e406 commit 0869b11

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

help_docs/method_summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ self.get_path_of_downloaded_file(file, browser=False)
311311

312312
self.is_downloaded_file_present(file, browser=False)
313313

314+
self.delete_downloaded_file_if_present(file, browser=False)
315+
314316
self.assert_downloaded_file(file, timeout=None, browser=False)
315317

316318
self.assert_true(expr, msg=None)

seleniumbase/fixtures/base_case.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,6 +3129,27 @@ def is_downloaded_file_present(self, file, browser=False):
31293129
return os.path.exists(self.get_path_of_downloaded_file(
31303130
file, browser=browser))
31313131

3132+
def delete_downloaded_file_if_present(self, file, browser=False):
3133+
""" Deletes the file from the [Downloads Folder] if the file exists.
3134+
For browser click-initiated downloads, SeleniumBase will override
3135+
the system [Downloads Folder] to be "./downloaded_files/",
3136+
but that path can't be overridden when using Safari, IE,
3137+
or Chromium Guest Mode, which keeps the default system path.
3138+
self.download_file(file_url) will always use "./downloaded_files/".
3139+
@Params
3140+
file - The filename to be deleted from the [Downloads Folder].
3141+
browser - If True, uses the path set by click-initiated downloads.
3142+
If False, uses the self.download_file(file_url) path.
3143+
Those paths are often the same. (browser-dependent)
3144+
(Default: False).
3145+
"""
3146+
if self.is_downloaded_file_present(file, browser=browser):
3147+
file_path = self.get_path_of_downloaded_file(file, browser=browser)
3148+
try:
3149+
os.remove(file_path)
3150+
except Exception:
3151+
pass
3152+
31323153
def assert_downloaded_file(self, file, timeout=None, browser=False):
31333154
""" Asserts that the file exists in SeleniumBase's [Downloads Folder].
31343155
For browser click-initiated downloads, SeleniumBase will override

0 commit comments

Comments
 (0)