@@ -752,6 +752,18 @@ def reset_permissions(self):
752752 driver = driver .cdp_base
753753 return self .loop .run_until_complete (driver .reset_permissions ())
754754
755+ def get_all_urls (self , absolute = True ):
756+ """
757+ Convenience function that returns all links (a,link,img,script,meta).
758+ :param absolute:
759+ Try to build all the links in absolute form
760+ instead of "as is", often relative.
761+ :return: List of URLs.
762+ """
763+ return self .loop .run_until_complete (
764+ self .page .get_all_urls (absolute = absolute )
765+ )
766+
755767 def get_all_cookies (self , * args , ** kwargs ):
756768 driver = self .driver
757769 if hasattr (driver , "cdp_base" ):
@@ -1725,6 +1737,80 @@ def post_message(self, message, duration=None, pause=True, style="info"):
17251737 duration = float (duration ) + 0.15
17261738 time .sleep (float (duration ))
17271739
1740+ def download_file (self , file_url ):
1741+ """Download a file from a URL.
1742+ The default download location is: "./downloaded_files/"."""
1743+ self .loop .run_until_complete (
1744+ self .page .download_file (file_url )
1745+ )
1746+
1747+ def save_file_as (self , file_url , new_file_name ):
1748+ """Download a file from a URL and rename it.
1749+ The default download location is: "./downloaded_files/"."""
1750+ self .loop .run_until_complete (
1751+ self .page .download_file (file_url , new_file_name )
1752+ )
1753+
1754+ def assert_downloaded_file (self , file , timeout = None ):
1755+ """Asserts that the file exists in SeleniumBase's [Downloads Folder].
1756+ For browser click-initiated downloads, SeleniumBase will override
1757+ the system [Downloads Folder] to be "./downloaded_files/".
1758+ @Params
1759+ file - The filename of the downloaded file.
1760+ timeout - The time (seconds) to wait for the download to complete.
1761+ browser - If True, uses the path set by click-initiated downloads."""
1762+ downloads_folder = constants .Files .DOWNLOADS_FOLDER
1763+ abs_path = os .path .abspath ("." )
1764+ downloads_path = os .path .join (abs_path , downloads_folder )
1765+ if not timeout :
1766+ timeout = settings .LARGE_TIMEOUT
1767+ start_ms = time .time () * 1000.0
1768+ stop_ms = start_ms + (timeout * 1000.0 )
1769+ downloaded_file_path = os .path .join (downloads_path , file )
1770+ found = False
1771+ for x in range (int (timeout )):
1772+ shared_utils .check_if_time_limit_exceeded ()
1773+ try :
1774+ self .assert_true (
1775+ os .path .exists (downloaded_file_path ),
1776+ "File [%s] was not found in the downloads folder [%s]!"
1777+ % (file , downloads_path ),
1778+ )
1779+ found = True
1780+ break
1781+ except Exception :
1782+ now_ms = time .time () * 1000.0
1783+ if now_ms >= stop_ms :
1784+ break
1785+ time .sleep (1 )
1786+ if not found and not os .path .exists (downloaded_file_path ):
1787+ plural = "s"
1788+ if timeout == 1 :
1789+ plural = ""
1790+ message = (
1791+ "File {%s} was not found in the downloads folder {%s} "
1792+ "after %s second%s! (Or the download didn't complete!)"
1793+ % (file , downloads_path , timeout , plural )
1794+ )
1795+ from seleniumbase .common .exceptions import NoSuchFileException
1796+ raise NoSuchFileException (message )
1797+
1798+ def get_path_of_downloaded_file (self , file ):
1799+ """This assumes the default location of SeleniumBase downloads,
1800+ which is the "./downloaded_files/" folder where scripts run."""
1801+ downloads_folder = constants .Files .DOWNLOADS_FOLDER
1802+ abs_path = os .path .abspath ("." )
1803+ downloads_path = os .path .join (abs_path , downloads_folder )
1804+ return os .path .join (downloads_path , file )
1805+
1806+ def set_download_path (self , path ):
1807+ """Set a new download path for click-initiated downloads.
1808+ (For Pure CDP Mode sync format only! -> sb_cdp.)
1809+ The default download location is: "./downloaded_files/".
1810+ Convenience methods such as assert_downloaded_file(file)
1811+ will still expect the default location."""
1812+ self .loop .run_until_complete (self .page .set_download_path (path ))
1813+
17281814 def set_locale (self , locale ):
17291815 """(Settings will take effect on the next page load)"""
17301816 self .loop .run_until_complete (self .page .set_locale (locale ))
@@ -3233,13 +3319,23 @@ def assert_text_not_visible(self, text, selector="body", timeout=None):
32333319 )
32343320 return True
32353321
3236- def assert_true (self , expression ):
3322+ def assert_true (self , expression , msg = None ):
32373323 if not expression :
3238- raise AssertionError ("%s is not true" % expression )
3324+ if not msg :
3325+ raise AssertionError ("%s is not true" % expression )
3326+ else :
3327+ raise AssertionError (
3328+ "%s is not true. (%s)" % (expression , msg )
3329+ )
32393330
3240- def assert_false (self , expression ):
3331+ def assert_false (self , expression , msg = None ):
32413332 if expression :
3242- raise AssertionError ("%s is not false" % expression )
3333+ if not msg :
3334+ raise AssertionError ("%s is not false" % expression )
3335+ else :
3336+ raise AssertionError (
3337+ "%s is not false. (%s)" % (expression , msg )
3338+ )
32433339
32443340 def assert_equal (self , first , second ):
32453341 if first != second :
0 commit comments