Asserts that the file exists in SeleniumBase's [Downloads Folder]. For browser click-initiated downloads, SeleniumBase will override the system [Downloads Folder] to be "./downloaded_files/". @Params file - The filename of the downloaded file. timeout - Th
(self, file, timeout=None)
| 1792 | ) |
| 1793 | |
| 1794 | def assert_downloaded_file(self, file, timeout=None): |
| 1795 | """Asserts that the file exists in SeleniumBase's [Downloads Folder]. |
| 1796 | For browser click-initiated downloads, SeleniumBase will override |
| 1797 | the system [Downloads Folder] to be "./downloaded_files/". |
| 1798 | @Params |
| 1799 | file - The filename of the downloaded file. |
| 1800 | timeout - The time (seconds) to wait for the download to complete. |
| 1801 | browser - If True, uses the path set by click-initiated downloads.""" |
| 1802 | downloads_folder = constants.Files.DOWNLOADS_FOLDER |
| 1803 | abs_path = os.path.abspath(".") |
| 1804 | downloads_path = os.path.join(abs_path, downloads_folder) |
| 1805 | if not timeout: |
| 1806 | timeout = settings.LARGE_TIMEOUT |
| 1807 | start_ms = time.time() * 1000.0 |
| 1808 | stop_ms = start_ms + (timeout * 1000.0) |
| 1809 | downloaded_file_path = os.path.join(downloads_path, file) |
| 1810 | found = False |
| 1811 | for x in range(int(timeout)): |
| 1812 | shared_utils.check_if_time_limit_exceeded() |
| 1813 | try: |
| 1814 | self.assert_true( |
| 1815 | os.path.exists(downloaded_file_path), |
| 1816 | "File [%s] was not found in the downloads folder [%s]!" |
| 1817 | % (file, downloads_path), |
| 1818 | ) |
| 1819 | found = True |
| 1820 | break |
| 1821 | except Exception: |
| 1822 | now_ms = time.time() * 1000.0 |
| 1823 | if now_ms >= stop_ms: |
| 1824 | break |
| 1825 | time.sleep(1) |
| 1826 | if not found and not os.path.exists(downloaded_file_path): |
| 1827 | plural = "s" |
| 1828 | if timeout == 1: |
| 1829 | plural = "" |
| 1830 | message = ( |
| 1831 | "File {%s} was not found in the downloads folder {%s} " |
| 1832 | "after %s second%s! (Or the download didn't complete!)" |
| 1833 | % (file, downloads_path, timeout, plural) |
| 1834 | ) |
| 1835 | from seleniumbase.common.exceptions import NoSuchFileException |
| 1836 | raise NoSuchFileException(message) |
| 1837 | |
| 1838 | def get_path_of_downloaded_file(self, file): |
| 1839 | """This assumes the default location of SeleniumBase downloads, |