|
1 | 1 | import logging |
| 2 | +import re |
2 | 3 | from typing import Literal |
3 | 4 |
|
4 | | -from selenium.common.exceptions import TimeoutException |
| 5 | +from selenium.common.exceptions import StaleElementReferenceException, TimeoutException |
5 | 6 | from selenium.webdriver import ActionChains, Firefox |
6 | 7 | from selenium.webdriver.common.by import By |
7 | 8 | from selenium.webdriver.common.keys import Keys |
@@ -245,6 +246,21 @@ def click_download_button(self) -> BasePage: |
245 | 246 | self.get_download_button().click() |
246 | 247 | return self |
247 | 248 |
|
| 249 | + @BasePage.context_chrome |
| 250 | + def set_always_open_similar_files(self) -> BasePage: |
| 251 | + """ |
| 252 | + From the downloads panel, right-click the most recent download and set 'Always Open Similar Files'. |
| 253 | + """ |
| 254 | + downloads_button = self.get_download_button() |
| 255 | + downloads_button.click() |
| 256 | + |
| 257 | + # Locate the latest downloaded file in the panel, open context menu and choose 'Always Open Similar Files' |
| 258 | + download_item = self.get_element("download-panel-item") |
| 259 | + self.context_click(download_item) |
| 260 | + self.context_menu.get_element("context-menu-always-open-similar-files").click() |
| 261 | + |
| 262 | + return self |
| 263 | + |
248 | 264 | @BasePage.context_chrome |
249 | 265 | def wait_for_download_animation_finish( |
250 | 266 | self, downloads_button: WebElement |
@@ -275,27 +291,40 @@ def click_file_download_warning_panel(self) -> BasePage: |
275 | 291 | self.click_on("file-download-warning-button") |
276 | 292 | return self |
277 | 293 |
|
278 | | - def wait_for_item_to_download(self, filename: str) -> BasePage: |
| 294 | + def wait_for_item_to_download(self) -> BasePage: |
279 | 295 | """ |
280 | | - Check the downloads tool in the toolbar to wait for a given file to download |
| 296 | + Wait for download elements to be present. |
| 297 | +
|
281 | 298 | """ |
282 | | - original_timeout = self.driver.timeouts.implicit_wait |
283 | | - try: |
284 | | - # Whatever our timeout, we want to lengthen it because downloads |
285 | | - self.driver.implicitly_wait(original_timeout * 2) |
286 | | - self.element_visible("downloads-item-by-file", labels=[filename]) |
287 | | - self.expect_not( |
288 | | - EC.element_attribute_to_include( |
289 | | - self.get_selector("downloads-button"), "animate" |
290 | | - ) |
291 | | - ) |
292 | | - with self.driver.context(self.context_id): |
293 | | - self.driver.execute_script( |
294 | | - "arguments[0].setAttribute('hidden', true)", |
295 | | - self.get_element("downloads-button"), |
296 | | - ) |
297 | | - finally: |
298 | | - self.driver.implicitly_wait(original_timeout) |
| 299 | + self.element_visible("download-target-element") |
| 300 | + return self |
| 301 | + |
| 302 | + @BasePage.context_chrome |
| 303 | + def verify_download_name(self, expected_pattern: str) -> BasePage: |
| 304 | + """ |
| 305 | + Verify download name matches expected pattern. |
| 306 | + Argument: |
| 307 | + expected_pattern: Regex pattern to match against download name |
| 308 | + """ |
| 309 | + download_name = self.get_element("download-target-element") |
| 310 | + download_value = download_name.get_attribute("value") |
| 311 | + assert re.match(expected_pattern, download_value), ( |
| 312 | + f"The download name is incorrect: {download_value}" |
| 313 | + ) |
| 314 | + return self |
| 315 | + |
| 316 | + @BasePage.context_chrome |
| 317 | + def wait_for_download_completion(self) -> BasePage: |
| 318 | + """Wait until the most recent download reaches 100% progress.""" |
| 319 | + |
| 320 | + def _download_complete(_): |
| 321 | + try: |
| 322 | + element = self.get_element("download-progress-element") |
| 323 | + return element.get_attribute("value") == "100" |
| 324 | + except StaleElementReferenceException: |
| 325 | + return False |
| 326 | + |
| 327 | + self.wait.until(_download_complete) |
299 | 328 | return self |
300 | 329 |
|
301 | 330 | @BasePage.context_chrome |
|
0 commit comments