@@ -3010,7 +3010,7 @@ def save_element_as_image_file(
3010
3010
def download_file (self , file_url , destination_folder = None ):
3011
3011
""" Downloads the file from the url to the destination folder.
3012
3012
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") """
3014
3014
if not destination_folder :
3015
3015
destination_folder = constants .Files .DOWNLOADS_FOLDER
3016
3016
if not os .path .exists (destination_folder ):
@@ -3028,49 +3028,94 @@ def save_file_as(self, file_url, new_file_name, destination_folder=None):
3028
3028
def save_data_as (self , data , file_name , destination_folder = None ):
3029
3029
""" Saves the data specified to a file of the name specified.
3030
3030
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") """
3032
3032
if not destination_folder :
3033
3033
destination_folder = constants .Files .DOWNLOADS_FOLDER
3034
3034
page_utils ._save_data_as (data , destination_folder , file_name )
3035
3035
3036
3036
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. """
3039
3042
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 :
3046
3044
# Guest Mode (non-headless) can force the default downloads path
3047
3045
return os .path .join (os .path .expanduser ('~' ), 'downloads' )
3048
3046
else :
3049
3047
from seleniumbase .core import download_helper
3050
3048
return download_helper .get_downloads_folder ()
3051
3049
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' )
3059
3066
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
+ """
3062
3106
self .__check_scope ()
3063
3107
if not timeout :
3064
3108
timeout = settings .LARGE_TIMEOUT
3065
3109
if self .timeout_multiplier and timeout == settings .LARGE_TIMEOUT :
3066
3110
timeout = self .__get_new_timeout (timeout )
3067
3111
start_ms = time .time () * 1000.0
3068
3112
stop_ms = start_ms + (timeout * 1000.0 )
3113
+ downloaded_file_path = self .get_path_of_downloaded_file (file , browser )
3069
3114
for x in range (int (timeout )):
3070
3115
shared_utils .check_if_time_limit_exceeded ()
3071
3116
try :
3072
3117
self .assertTrue (
3073
- os .path .exists (self . get_path_of_downloaded_file ( file ) ),
3118
+ os .path .exists (downloaded_file_path ),
3074
3119
"File [%s] was not found in the downloads folder [%s]!"
3075
3120
"" % (file , self .get_downloads_folder ()))
3076
3121
if self .demo_mode :
@@ -3083,7 +3128,7 @@ def assert_downloaded_file(self, file, timeout=None):
3083
3128
if now_ms >= stop_ms :
3084
3129
break
3085
3130
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 ):
3087
3132
message = (
3088
3133
"File {%s} was not found in the downloads folder {%s} "
3089
3134
"after %s seconds! (Or the download didn't complete!)"
0 commit comments