Skip to content

Commit ec668c2

Browse files
committed
Distinguish folders for downloading files, and expand methods
1 parent abe47f9 commit ec668c2

File tree

1 file changed

+66
-21
lines changed

1 file changed

+66
-21
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,7 +3010,7 @@ def save_element_as_image_file(
30103010
def download_file(self, file_url, destination_folder=None):
30113011
""" Downloads the file from the url to the destination folder.
30123012
If no destination folder is specified, the default one is used.
3013-
(The default downloads folder = "./downloaded_files") """
3013+
(The default [Downloads Folder] = "./downloaded_files") """
30143014
if not destination_folder:
30153015
destination_folder = constants.Files.DOWNLOADS_FOLDER
30163016
if not os.path.exists(destination_folder):
@@ -3028,49 +3028,94 @@ def save_file_as(self, file_url, new_file_name, destination_folder=None):
30283028
def save_data_as(self, data, file_name, destination_folder=None):
30293029
""" Saves the data specified to a file of the name specified.
30303030
If no destination folder is specified, the default one is used.
3031-
(The default downloads folder = "./downloaded_files") """
3031+
(The default [Downloads Folder] = "./downloaded_files") """
30323032
if not destination_folder:
30333033
destination_folder = constants.Files.DOWNLOADS_FOLDER
30343034
page_utils._save_data_as(data, destination_folder, file_name)
30353035

30363036
def get_downloads_folder(self):
3037-
""" Returns the OS path of the "downloads/" folder.
3038-
Chromium Guest Mode overwrites the path set by SeleniumBase. """
3037+
""" Returns the path of the SeleniumBase "downloaded_files/" folder.
3038+
Calling self.download_file(file_url) will put that file in here.
3039+
With the exception of Safari, IE, and Chromium Guest Mode,
3040+
any clicks that download files will also use this folder
3041+
rather than using the browser's default "downloads/" path. """
30393042
self.__check_scope()
3040-
sys_plat = sys.platform
3041-
chromium = False
3042-
if self.browser in ("chrome", "edge", "opera"):
3043-
chromium = True
3044-
if chromium and self.guest_mode and "linux" not in sys_plat and (
3045-
not self.headless):
3043+
if self.is_chromium() and self.guest_mode and not self.headless:
30463044
# Guest Mode (non-headless) can force the default downloads path
30473045
return os.path.join(os.path.expanduser('~'), 'downloads')
30483046
else:
30493047
from seleniumbase.core import download_helper
30503048
return download_helper.get_downloads_folder()
30513049

3052-
def get_path_of_downloaded_file(self, file):
3053-
""" Returns the OS path of the downloaded file. """
3054-
return os.path.join(self.get_downloads_folder(), file)
3055-
3056-
def is_downloaded_file_present(self, file):
3057-
""" Checks if the file exists in the Downloads Folder. """
3058-
return os.path.exists(self.get_path_of_downloaded_file(file))
3050+
def get_browser_downloads_folder(self):
3051+
""" Returns the path that is used when a click initiates a download.
3052+
SeleniumBase overrides the system path to be "downloaded_files/"
3053+
The path can't be changed on Safari, IE, or Chromium Guest Mode.
3054+
"""
3055+
self.__check_scope()
3056+
if self.is_chromium() and self.guest_mode and not self.headless:
3057+
# Guest Mode (non-headless) can force the default downloads path
3058+
return os.path.join(os.path.expanduser('~'), 'downloads')
3059+
elif self.browser == "safari" or self.browser == "ie":
3060+
# Can't change the system [Downloads Folder] on Safari or IE
3061+
return os.path.join(os.path.expanduser('~'), 'downloads')
3062+
else:
3063+
from seleniumbase.core import download_helper
3064+
return download_helper.get_downloads_folder()
3065+
return os.path.join(os.path.expanduser('~'), 'downloads')
30593066

3060-
def assert_downloaded_file(self, file, timeout=None):
3061-
""" Asserts that the file exists in the Downloads Folder. """
3067+
def get_path_of_downloaded_file(self, file, browser=False):
3068+
""" Returns the OS path of the downloaded file. """
3069+
if browser:
3070+
return os.path.join(self.get_browser_downloads_folder(), file)
3071+
else:
3072+
return os.path.join(self.get_downloads_folder(), file)
3073+
3074+
def is_downloaded_file_present(self, file, browser=False):
3075+
""" Returns True if the file exists in the pre-set [Downloads Folder].
3076+
For browser click-initiated downloads, SeleniumBase will override
3077+
the system [Downloads Folder] to be "./downloaded_files/",
3078+
but that path can't be overridden when using Safari, IE,
3079+
or Chromium Guest Mode, which keeps the default system path.
3080+
self.download_file(file_url) will always use "./downloaded_files/".
3081+
@Params
3082+
file - The filename of the downloaded file.
3083+
browser - If True, uses the path set by click-initiated downloads.
3084+
If False, uses the self.download_file(file_url) path.
3085+
Those paths are often the same. (browser-dependent)
3086+
(Default: False).
3087+
"""
3088+
return os.path.exists(self.get_path_of_downloaded_file(
3089+
file, browser=browser))
3090+
3091+
def assert_downloaded_file(self, file, timeout=None, browser=False):
3092+
""" Asserts that the file exists in SeleniumBase's [Downloads Folder].
3093+
For browser click-initiated downloads, SeleniumBase will override
3094+
the system [Downloads Folder] to be "./downloaded_files/",
3095+
but that path can't be overridden when using Safari, IE,
3096+
or Chromium Guest Mode, which keeps the default system path.
3097+
self.download_file(file_url) will always use "./downloaded_files/".
3098+
@Params
3099+
file - The filename of the downloaded file.
3100+
timeout - The time (seconds) to wait for the download to complete.
3101+
browser - If True, uses the path set by click-initiated downloads.
3102+
If False, uses the self.download_file(file_url) path.
3103+
Those paths are often the same. (browser-dependent)
3104+
(Default: False).
3105+
"""
30623106
self.__check_scope()
30633107
if not timeout:
30643108
timeout = settings.LARGE_TIMEOUT
30653109
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
30663110
timeout = self.__get_new_timeout(timeout)
30673111
start_ms = time.time() * 1000.0
30683112
stop_ms = start_ms + (timeout * 1000.0)
3113+
downloaded_file_path = self.get_path_of_downloaded_file(file, browser)
30693114
for x in range(int(timeout)):
30703115
shared_utils.check_if_time_limit_exceeded()
30713116
try:
30723117
self.assertTrue(
3073-
os.path.exists(self.get_path_of_downloaded_file(file)),
3118+
os.path.exists(downloaded_file_path),
30743119
"File [%s] was not found in the downloads folder [%s]!"
30753120
"" % (file, self.get_downloads_folder()))
30763121
if self.demo_mode:
@@ -3083,7 +3128,7 @@ def assert_downloaded_file(self, file, timeout=None):
30833128
if now_ms >= stop_ms:
30843129
break
30853130
time.sleep(1)
3086-
if not os.path.exists(self.get_path_of_downloaded_file(file)):
3131+
if not os.path.exists(downloaded_file_path):
30873132
message = (
30883133
"File {%s} was not found in the downloads folder {%s} "
30893134
"after %s seconds! (Or the download didn't complete!)"

0 commit comments

Comments
 (0)